Hi, I'm working on conversion my b4a app to b4i and have encrypt / decrypt problem
I already have a code that does encryption / decryption on Android and on VB.NET on my server.
b4a
VB.NET
From what I learnt already (I'm new to crypt stuff and to b4i) that b4i doesn't support KeyGenerator.
So what are my options?
I already found a code that works on b4a and does encrypt / decrypt [URL]https://www.b4x.com/android/fo...yption-for-b4j-b4a-and-b4i-also-in-url.58773/[/url]
but how adapt this code to VB.NET? Or keep my VB.NET and adjust b4a code?
Thanks in advance.
Alex
I already have a code that does encryption / decryption on Android and on VB.NET on my server.
b4a
B4X:
Sub Encrypt(dataToEncrypt As String ) As String
Dim kg As KeyGenerator
Dim c As Cipher
Dim B64 As Base64
Dim bconv As ByteConverter
Dim data(0) As Byte
Dim iv(0) As Byte
iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
c.Initialize("DESEDE/CBC/PKCS5Padding")
c.InitialisationVector = iv
kg.Initialize("DESEDE")
kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))
data = bconv.StringToBytes(dataToEncrypt, "ASCII")
data = c.Encrypt(data, kg.Key, True)
Return B64.EncodeBtoS(data, 0, data.Length)
End Sub
Sub Decrypt(encryptedData As String ) As String
Dim kg As KeyGenerator
Dim c As Cipher
Dim B64 As Base64
Dim bconv As ByteConverter
Dim data(0) As Byte
Dim iv(0) As Byte
iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
c.Initialize("DESEDE/CBC/PKCS5Padding")
c.InitialisationVector = iv
kg.Initialize("DESEDE")
kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))
data = B64.DecodeStoB(encryptedData)
data = c.Decrypt(data, kg.Key, True)
Return bconv.StringFromBytes(data, "ASCII")
End Sub
VB.NET
B4X:
Public Function Encrypt(dataToEncrypt As String) As String
Try
Dim buffer As Byte() = Encoding.ASCII.GetBytes(dataToEncrypt)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}
des.Key = ASCIIEncoding.UTF8.GetBytes("1234567890123456")
Dim result = System.Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
des.Clear()
Return result
Catch ex As Exception
Dim s As String
s = ex.Message
Return -1 'ex.Message
End Try
End Function
Public Function Decrypt(encryptedData As String) As String
Try
Dim buffer As Byte() = Convert.FromBase64String(encryptedData)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}
des.Key = ASCIIEncoding.UTF8.GetBytes("1234567890123456")
Dim result As String = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
des.Clear()
Return result
Catch ex As Exception
Dim s As String
s = ex.Message
Return -1 'ex.Message
End Try
End Function
From what I learnt already (I'm new to crypt stuff and to b4i) that b4i doesn't support KeyGenerator.
So what are my options?
I already found a code that works on b4a and does encrypt / decrypt [URL]https://www.b4x.com/android/fo...yption-for-b4j-b4a-and-b4i-also-in-url.58773/[/url]
but how adapt this code to VB.NET? Or keep my VB.NET and adjust b4a code?
Thanks in advance.
Alex