iPHone - AES 256 encryption without padding

不问归期 提交于 2019-12-01 14:45:44

Block ciphers will always be a multiple of their block size. When data does not fit exactly into the cipher stream it is padded. So, there's no need to disable padding.

The padding is kind of important.

http://www.vbdotnetheaven.com/UploadFile/gsparamasivam/cryp04112005063256AM/cryp.aspx

I'd ask why you wanted to get rid of it but I suspect you probably just need to understand why it's there.

Of course if you really wanted to get rid of the padding, just make your data size be a multiple of the cipher key length.

It seems you are using this piece of code

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );

size_t numBytesEncrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      tempkey, kCCKeySizeAES256,
                                      (void*)IV /* initialization vector (optional) */,
                                      input_raw_data, data_length, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted );

I've also gone through the same problem and I found the solution which is do not use the above function it will add extra bytes in encrypting. Just use the two functions instead of this one. Here is my solution

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );

size_t numBytesEncrypted = 0;

CCCryptorRef ccRef;
CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, tempkey, kCCKeySizeAES256, IV, &ccRef);
CCCryptorStatus cryptStatus = CCCryptorUpdate(ccRef, input_raw_data, data_length, buffer, bufferSize, &numBytesEncrypted);

CCCryptorRelease(ccRef);
if( cryptStatus == kCCSuccess )
{
  //the returned NSData takes ownership of the buffer and will free it on deallocation
  return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}

The option kCCOptionPKCS7Padding does this for you (I refer to the pastie code). If, say, you encrypt 17 bytes then then resulting ciphertext will be 32 bytes (the next multiple of 16): we need 16 bytes per block; if we have a text of 16 bytes then the ciphertext will also 32 bytes, because the padding has to be "uniquely removable" :we add x bytes with value x, for 1 <= x <= 16 in this case. This is done automatically (and checked for errors) with that option during decryption. If you encrypt/decrypt with CBC (it's unclear to me whether that is the case here, I suspect not) we add another random IV block at the beginning of the ciphertext, and this is to ensure that encrypting the same plaintext under the same key later will most likely result in different ciphertexts. So this is recommended practice. If you do not want padding, you can use the block cipher in a streaming mode, like counter mode or CFB-mode. You still get a little expansion because you have to add an IV or nonce as well, also 16 bytes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!