1
+ (NSData *)doCipher:(NSData *)dataIn
                  iv:(NSData *)iv
                 key:(NSData *)symmetricKey
             context:(CCOperation)encryptOrDecrypt // kCCEncrypt or kCCDecrypt
               error:(NSError **)error
{
    CCCryptorStatus ccStatus   = kCCSuccess;
    size_t          cryptBytes = 0;
    NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

    ccStatus = CCCrypt( encryptOrDecrypt,
                       kCCAlgorithmAES128,
                       0, //kCCOptionPKCS7Padding,
                       symmetricKey.bytes,
                       kCCKeySizeAES128,
                       iv.bytes,
                       dataIn.bytes,
                       dataIn.length,
                       dataOut.mutableBytes,
                       dataOut.length,
                       &cryptBytes);

    if (ccStatus == kCCSuccess) {
        dataOut.length = cryptBytes;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:@"kEncryptionError"
                                         code:ccStatus
                                     userInfo:nil];
        }
        dataOut = nil;
    }

    return dataOut;
}

I have dataIn as 154 bytes of Data, key and Iv having 16 bytes of data when i am doing encryption it is giving -4303 error.(AES128 With CBC In IOS)

John Tracid
  • 3,836
  • 3
  • 22
  • 33
Sarath
  • 11
  • 2
  • Alignment error means there is something wrong with the sizes. Please check my answer for more details: https://stackoverflow.com/questions/35921254/aes-ecb-ios-encrypt/48559334#48559334 – Joanne Feb 01 '18 at 09:42

1 Answers1

0

Error code -4303 is kCCAlignmentError. From documentation:

When decrypting, or when encrypting with a block cipher with padding disabled, kCCAlignmentError will be returned if the total number of bytes provided to CCCryptUpdate() is not an integral multiple of the current algorithm's block size.

You should check if your input data size is correct.

John Tracid
  • 3,836
  • 3
  • 22
  • 33