Article by: Ehioze Iweka
Data encryption is one of the ways of ensuring that data from your App is transported securely for processing via a service. I will be shedding a little light on data encryption using AES algorithm for anyone interested in encrypting data sent from their mobile application.
What is AES Encryption?
The Advanced Encryption Standard (AES), also known by its original name Rijndael, is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001.[4]. Read more here.
How is AES Encryption Performed
Performing AES encryption in iOS is quite straight forward depending on the the encryption mode. There are basically two modes: ECB (Electronic Code Block) and CBC (Cipher Block Chaining). The major difference is that the CBC makes use of an initialization vector while the ECB does not. To perform encryption in the CBC mode, the following are required:
1. Secret Key: This is a randomly generated 16,24,or 32 digit key which must be used for both the encryption and decryption purposes. This key can be in base64 format or plain text depending on what’s best for you, but the rule is whatever format is used for encryption has to be the same format used for decryption, in other to get the same result.
2. Initialization Vector: This is a randomly generated 16 digit string, numeric or alphanumeric value used in the case of a CBC encryption
3.The text or data to be encrypted. As observed, the item for encryption can be a text string, a data or even an image data.
Assuming we already have our Secret Key defined in any format discussed above, lets proceed to generate our initialization vector. Below is a class method that randomly generates an initialization vector, of any specified length, and returns the result as a base64 encoded string.
Initialization vector generation
with the generated Initialization Vector(IV) and Secret Key you can now AES encrypt and decrypt your data in the following ways:
encrypting and decrypting method
You will need to add the AQToolKit third party library to your project, which is where the encryption algorithm is written, to avoid errors when you build your project.
Finally you have to implement the getKey and getIV methods used in your encrypt and decrypt method above. These methods just ensures that your IV and KEY are sent to the encryption method as a Data and not String. I have added the snippets below too:
You can convert your encrypted or decrypted data to base64 format using this code snippet:
where encrypted is the result from your encryption by the encrypt method above.
CONCLUSION
Data encryption using AES algorithm is very easy to implement if only the rules are followed when doing it. I hope this helps someone out there. Feel free to drop comments, and get across to me, for further clarification or assistance.
Ehioze Iweka
Email iehioze@gmail.com,
Twitter (ehis_iweka)
LinkedIn: www.linkedin.com/in/ehiozeiweka