thndrkiss
thndrkiss

Reputation: 4595

aes encrytion - content size not in multiples of 16bytes

i have a data which his close to 735 bytes, when i encrypt and send it to server server is able to encrypt until say 720 (in multiples of 16) but not anything beyond that, how to fix this problem, all i need is a kind of solution which will clear this problem.

NSMutableData *cipherData = [NSMutableData dataWithLength: content.length + kCCBlockSizeAES128];
    CCCryptorStatus
    result = CCCrypt(((shouldEncrypt)?kCCEncrypt:kCCDecrypt), // operation
                     kCCAlgorithmAES128, // Algorithm - AES128 means the block size, not the keysize 
                     kCCOptionECBMode, // options
                     key.bytes, // key
                     kCCKeySizeAES256, // keylength
                     NULL,// iv
                     content.bytes, // dataIn
                     content.length, // dataInLength,
                     cipherData.mutableBytes, // dataOut
                     cipherData.length, // dataOutAvailable
                     &outLength); // dataOutMoved

Upvotes: 0

Views: 53

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94038

In options, (binary) OR the kCCOptionPKCS7Padding flag to the kCCOptionECBMode flag.

This will automatically pad the last block to the block size. Note that this will also add a full block if a integral number of blocks is present (otherwise the data cannot be distinguished from the padding).

Upvotes: 1

Related Questions