« Tip of the day: Conne… | Home | MDI Window Background… »

Tip of the day: AES 256 CBC on iOS for Xojo

Today I made an example for a client to show how to do AES 256bit CBC encryption on iOS using CommonCrypto library and the MBS Xojo Encryption Kit.

Our Encryption Kit wraps the CommonCrypto framework from Apple which is available for iOS (2.0 or newer) and macOS (10.4 or newer). It also wraps zlib library to compress and decompress data.

The compression and encryption functions are than used in a database connector. That's a proxy to connect to a database on a server and serialize the requests and record sets to send them through the network.

So here is some sample code for AES 256 CBC:

dim MyVal as text = "If you can read this text then process of encryption and decryption is working well." dim mbMyVal as xojo.Core.MemoryBlock = xojo.core.TextEncoding.utf8.ConvertTextToData(MyVal) dim MyPwd as text = "Passphrase goes here" dim mbMyPwd as xojo.Core.MemoryBlock = CommonCryptoMB.Hash(CommonCryptoMB.Hashes.SHA256,MyPwd) System.DebugLog "Test value and passphrase created." dim iv as xojo.Core.MemoryBlock dim cryptor as new CommonCryptorMB(CommonCryptoMB.CryptoOperation.Encrypt, CommonCryptoMB.CryptoMode.kCCModeCBC, _ CommonCryptoMB.CryptoAlgorithm.AES, CommonCryptoMB.CryptoPadding.PKCS7, mbMyPwd, iv) dim enData1 as xojo.Core.MemoryBlock = Cryptor.Update(mbMyVal) dim enData2 as xojo.Core.MemoryBlock = cryptor.Final1 dim enData as new xojo.Core.MutableMemoryBlock(enData1) enData.Append enData2 System.DebugLog "Encrypted data value created." System.DebugLog "Encrypted data: '" + CommonCryptoMB.EncodeHex(enData) + "'" dim decryptor as new CommonCryptorMB(CommonCryptoMB.CryptoOperation.Decrypt, CommonCryptoMB.CryptoMode.kCCModeCBC, _ CommonCryptoMB.CryptoAlgorithm.AES, CommonCryptoMB.CryptoPadding.PKCS7, mbMyPwd, iv) dim result1 as xojo.Core.MemoryBlock = Cryptor.Update(enData) dim result2 as xojo.Core.MemoryBlock = cryptor.Final1 dim result as new xojo.Core.MutableMemoryBlock(result1) result.Append result2 System.DebugLog "Result: '" + xojo.core.TextEncoding.UTF8.ConvertDataToText(result) + "'"

The same code works fine on macOS. For cross platform encryption for Mac, Windows and Linux, I would recommend our CipherMBS class.
07 02 17 - 22:36