I have used Einhugur (AES CBC) to encrypt sensitive fields in my databases on desktop, console and web for years. Now I want to create an iOS version of my app and decrypt those fields.
Here is my dilemma:
- MBS Encryption Kit works, but supports only iOS and macOS ie no Windows or Linux
- Einhugur and M_Crypto support only desktop, console and web ie no iOS
So I need a multi-provider solution ie MBS Encryption Kit and one other. So I wrote a simple piece of code to encrypt a sample string with a password key and an IV Key, then HexEncode it, assuming they would all return the same Hex. Boy was I wrong! Every system produces different Hex results. Given they are all using public libraries behind the scenes, either my code is wrong or I am missing something. The Hex code is in the comment line after each encryption.
As MBS Encryption Kit is the only game in town for iOS encryption (that I know of), I need a Windows, Linux encryption solution that can encrypt/decrypt the text in the same way as the MBS Encryption Kit. I was hoping M_Crypto was this solution.
Can someone either tell me what I am doing wrong or inform me of another cross-platform solution that includes iOS?
[code]Const kInputText As String = “Some Text to encrypt
” '> 0 bytes and less than 4 GBytes
Dim kMBInputText As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(kInputText.ToText)
Const kPassword As String = “12345678901234567890123456789012” '32 bytes
Dim kPasswordText As Text = kPassword.ToText
Dim mbPasswordHash As Xojo.Core.MemoryBlock = CommonCryptoMB.Hash(CommonCryptoMB.Hashes.SHA256, kPasswordText)
Dim mbPassword As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(kPassword.ToText)
Const kIVKey As String = “1234567890123456” '16 bytes
Dim mbIVKey As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(kIVKey.ToText)
Dim mbIVKeyEmpty As Xojo.Core.MemoryBlock
Dim returnText As Text
Dim returnString As String
'MBS Encryption Kit for macOS and iOS only
Dim Encryptor As New CommonCryptorMB(CommonCryptoMB.CryptoOperation.Encrypt, CommonCryptoMB.CryptoMode.kCCModeCBC, CommonCryptoMB.CryptoAlgorithm.AES, CommonCryptoMB.CryptoPadding.PKCS7, kPasswordText, mbIVKey)
Dim enData1 As Xojo.Core.MemoryBlock = Encryptor.Update(kMBInputText)
Dim enData2 As Xojo.Core.MemoryBlock = Encryptor.Final1
Dim EncryptedData As New Xojo.Core.MutableMemoryBlock(enData1)
EncryptedData.Append(enData2)
returnText = CommonCryptoMB.EncodeHex(encryptedData)
// 1B25F04DF6A1B0091AFDA65BFE2963BCB98E44277D21C06E8E19FD9A9AE6A1EE
'M_Crypto for macOS, Windows and Linux on desktop, console and web only
Dim bf As Blowfish_MTC
bf = New Blowfish_MTC(kPassword, Blowfish_MTC.Padding.PKCS)
bf.SetInitialVector kIVKey
returnString = EncodeHex(bf.EncryptCBC(kInputText))
// A641C0421611BB9B14753C62D6D181A5DAADAFB240A3FDCF
'M_Crypto for macOS, Windows and Linux on desktop, console and web only
Dim tempAES_MTC As AES_MTC
tempAES_MTC = New AES_MTC(kPasswordText, AES_MTC.EncryptionBits.Bits192, AES_MTC.Padding.PKCS)
tempAES_MTC.SetInitialVector(kIVKey)
returnString = EncodeHex(tempAES_MTC.EncryptCBC(kInputText))
// E006D62B6DB2734C2BB2105DC7B949471C2F8C771F1D979DBBEF6D987A883CA0 as Bits128
// 2163D24D8B14F0CE64AE87CB391E7BA96B07C88003C2851B247FAA7AD8F43C78 as Bits192
// 0B8B533D0C6F6A016A9FCB97C82B875513AFFA8640F966BFBD09A028AE0199ED as Bits256
'Einhugur for macOS, Windows and Linux on desktop, console and web only
Dim tempAES_CBC As AES_CBC
tempAES_CBC = New AES_CBC(kPassword, kIVKey)
returnString = EncodeHex(tempAES_CBC.Encrypt(kInputText) + tempAES_CBC.FinishEncrypt)
// 0B8B533D0C6F6A016A9FCB97C82B875572797074E280A6 'same as M_Crypto 256 bit for first 32 Hex bytes
'Einhugur for macOS, Windows and Linux on desktop, console and web only
Dim twoFish As New TwofishCBC(kPassword, 0, kIVKey) '0=Encrypt, 1=Decrypt, 2=Don’t know
returnString = EncodeHex(twoFish.Encrypt(kInputText) + twoFish.FinishEncrypt)
// E50E715116917951B659508FABD6F8CC72797074E280A6[/code]