iOS AES128 Encryption and Decryption

Why I am giving the following steps instead of direct link is. The link is changing frequently I guess or they are updating the article or code itself. I saw the method names got changed.

Follow the steps.

  1. Go to http://iphonedevcentral.blogspot.in.
  2. Top left corner, you see search box. Search for Strong Encryption.
  3. Read the article and down the data from the link provided.

 

Code:

kCCOptionECBMode : Electronic Code Book (ECB). Electronic Code Book (ECB) is a mode of operation for a block cipher, with the characteristic that each possible block of plaintext has a defined corresponding ciphertext value and vice versa. In other words, the same plaintext value will always result in the same ciphertext value. Electronic Code Book is used when a volume of plaintext is separated into several blocks of data, each of which is then encrypted independently of other blocks. In fact, Electronic Code Book has the ability to support a separate encryption key for each block type.

Cited the above definition from http://searchsecurity.techtarget.com/definition/Electronic-Code-Book.

CCCrypt is the apple api that actually encrypts or decrypts based on the parameters passed to it.

kCCAlgorithmAES128 is the algorithm to use to encrypt or decrypt. It will successfully encrypt or decrypt only 15 characters, because the buffer size accepts 16 characters. Last character space is for termination character ‘\0’.

If you send more than 15 characters then you will face issue in decryption. So in order to encrypt more than 15 characters then you need to pass kCCOptionECBMode in options. That will internally split the characters and encrypt and provide you the result. In decryption also you mention this mode.

   CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode + kCCOptionPKCS7Padding,

                                          keyPtr, kCCKeySizeAES256,

                                          NULL /* initialization vector (optional) */,

                                          [self bytes], dataLength, /* input */

                                          buffer, bufferSize, /* output */

                                          &numBytesEncrypted );

Useful Links:

  1. https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
  2. https://de.wikipedia.org/wiki/Electronic_Code_Book_Mode
  3. https://www.tutorialspoint.com/cryptography/block_cipher_modes_of_operation.htm
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: