| Start |
|---|
| AES encryption in CBC mode with a random IV (Swift 3.0) The iv is prefixed to the encrypted data
Inputs are the data and key are Data objects. If an encoded form such as Base64 if required convert to and/or from in the calling method. The key should be exactly 128-bits (16-bytes), 192-bits (24-bytes) or 256-bits (32-bytes) in length. If another key size is used an error will be thrown. PKCS#7 padding (opens new window) is set by default. This example requires Common Crypto This is example, not production code. Example usage: Example Output: Notes: For security the encrypted data also should have authentication, this example code does not provide that in order to be small and allow better interoperability for other platforms. Also missing is key derivation of the key from a password, it is suggested that PBKDF2 (opens new window) be used is text passwords are used as keying material. For robust production ready multi-platform encryption code see RNCryptor (opens new window). Updated to use throw/catch and multiple key sizes based on the provided key. |
| AES encryption in CBC mode with a random IV (Swift 2.3) The iv is prefixed to the encrypted data aesCBC128Encrypt will create a random IV and prefixed to the encrypted code. aesCBC128Decrypt will use the prefixed IV during decryption. Inputs are the data and key are Data objects. If an encoded form such as Base64 if required convert to and/or from in the calling method. The key should be exactly 128-bits (16-bytes). For other key sizes see the Swift 3.0 example. PKCS#7 padding is set by default. This example requires Common Crypto It is necessary to have a bridging header to the project: #import <CommonCrypto/CommonCrypto.h> Add the Security.framework to the project. See Swift 3 example for notes. This is example, not production code. Example usage: Example Output: |
| AES encryption in ECB mode with PKCS7 padding From Apple documentation for IV, This parameter is ignored if ECB mode is used or if a stream cipher algorithm is selected. |
Read Article
https://develop.horje.com/learn/1434/reference