0

I am developing App for Android and IOS

For android I am using "AES/CBC/NoPadding" Cipher and for IOS am using CommonCrypto with same algo/mode/padding as Android

I am initializing the ciphers with common key, for getting same result in both platforms

In Android , am using cipher.update(inpBuf, inpOffset, inpLen, outBuf, outOffset) for encrypting/decryption purpose, same way I want to do it in IOS.

I Have tried CCCryptorUpdate in IOS , but the result array was [0,0,0......0]

Please guide me, where am gone wrong?

Hitesh
  • 59
  • 4
  • 15
  • Unless your data to be encrypted is **always** a multiple of the block size (16-bytes for AES) you need to add padding. This can be done manually (not suggested) or by using a supplied padding option such as PKCS#7 (née PKCS#5). – zaph Jun 18 '16 at 13:21

1 Answers1

1

AES is a block cipher that encrypts data in chunks of 16 bytes. When calling update() all blocks of 16 bytes will be encrypted and any excessive bytes will wait around until the next update() call. When no more data needs encryption you finishes of the encryption with a doFinal() that flushes the last block + and applies any padding needed. Why are you using NoPadding? Are you ensuring you data matches the block size of AES yourself?

When decrypting in IOS you use the corosponding CCCryptorUpdate() and CCCryptorFinal(). Only after the CCCryptorFinal() you would have the full decrypted message. CCCryptorUpdate() might or might not return data, depending on how much is stuck in the buffer etc.

Ebbe M. Pedersen
  • 7,250
  • 3
  • 27
  • 47
  • 1
    There is also the one-shot `CCCrypt` function which can be used in place of the `CCCryptorCreate`, `CCCryptorUpdate()`, `CCCryptorFinal()` and `CCCryptorRelease` calls. – zaph Jun 18 '16 at 13:20