Hi All
I have this code that I am using with b4A, I need the equivalent for b4i as I am communicating with a .Net Backend system that has a matching algorithm
Thanks in advance
I have this code that I am using with b4A, I need the equivalent for b4i as I am communicating with a .Net Backend system that has a matching algorithm
B4X:
Sub AES_Encrypt(szInputToEncrypt As String, szKey As String, szIV As String) As String
Dim baInputdata() As Byte
Dim baEncryptedData(0) As Byte
Dim szEncryptedData As String
Dim baIV(ENC_KEY_LENGTH) As Byte
Dim baKey(ENC_KEY_LENGTH) As Byte
ENC_ConvertHexStringTo16ByteArray(szKey, baKey)
ENC_ConvertHexStringTo16ByteArray(szIV, baIV)
baInputdata = szInputToEncrypt.GetBytes("UTF8")
Dim kg As KeyGenerator ,c As Cipher
c.Initialize("AES/CBC/PKCS5Padding")
' CBC needs an initialisation vector
c.InitialisationVector = baIV
kg.Initialize("AES")
kg.KeyFromBytes(baKey) 'Key
baEncryptedData = c.Encrypt(baInputdata, kg.key, True)
szEncryptedData = GeneralLib.StringUtils1.EncodeBase64(baEncryptedData)
'ENC_MyMessagesLog(szEncryptedData)
Return szEncryptedData
End Sub
B4X:
Sub AES_Decrypt(szInputToDecrypt As String, szKey As String, szIV As String) As String
Dim baInputdata() As Byte
Dim baDecryptedData(0) As Byte
Dim szDeccryptedData As String
Dim baIV(ENC_KEY_LENGTH) As Byte
Dim baKey(ENC_KEY_LENGTH) As Byte
ENC_ConvertHexStringTo16ByteArray(szKey, baKey)
ENC_ConvertHexStringTo16ByteArray(szIV, baIV)
baInputdata = GeneralLib.StringUtils1.DecodeBase64(szInputToDecrypt)
Dim kg As KeyGenerator ,c As Cipher
c.Initialize("AES/CBC/PKCS5Padding")
' CBC needs an initialisation vector
c.InitialisationVector = baIV
kg.Initialize("AES")
kg.KeyFromBytes(baKey) 'Key
baDecryptedData = c.Decrypt(baInputdata, kg.key, True)
szDeccryptedData = GeneralLib.ByteConv.StringFromBytes(baDecryptedData, "UTF8")
Return szDeccryptedData
End Sub
Thanks in advance