Having problems with encryption between a VB.NET host program and a B4a app. Sure I am missing something fundamental as this is my first whack at encryption.
I first tried AES VB.NET stuff and B4XEncryption. Was not working. After much time in the forum I saw Erel post "B4XEncryption will only work with B4a, B4i and B4j. Use Encryption library."
So off to Encrption library and DES on VB.NET. First several hours spent learning that the password key MUST be exactly 8 characters. Why?
The DES and Encryption library are producing two very different results. Any ideas? Did I miss a post? I have looked at everything returned form a "Encryption" search.
B4a Code:
VB.NET Code:
I first tried AES VB.NET stuff and B4XEncryption. Was not working. After much time in the forum I saw Erel post "B4XEncryption will only work with B4a, B4i and B4j. Use Encryption library."
So off to Encrption library and DES on VB.NET. First several hours spent learning that the password key MUST be exactly 8 characters. Why?
The DES and Encryption library are producing two very different results. Any ideas? Did I miss a post? I have looked at everything returned form a "Encryption" search.
B4a Code:
B4X:
' Dim cipherText() As Byte = EncryptText(txPassword.Text,PwdKey)
' Dim cText As String = BytesToString(cipherText,0,cipherText.Length,"utf8")
Dim kg As KeyGenerator
Dim c As Cipher
c.Initialize("DES/ECB/NoPadding")
kg.Initialize("DES")
Dim b() As Byte = PwdKey.GetBytes("UTF8")
Log("key length = " & b.Length)
kg.KeyFromBytes(b)
Dim cText As String = padString(txPassword.Text)
Log("cText.len = " & cText.Length)
Dim data(0) As Byte = bConv.StringToBytes(cText,"UTF8")
Log("data length = " & data.Length)
data = c.Encrypt(data,kg.Key,False)
cText = bConv.HexFromBytes(data)
' this did not work either cText = BytesToString(data,0,data.Length,"utf8")
mLog("SB","cText = " & cText)
mLog("SB","rspPassword = " & rspPassword)
If rspPassword <> cText Then
ToastMessageShow("Invalid User Name or Password",False)
Job.Release
fInTrans = False
Return
End If
B4X:
Imports System.Security.Cryptography
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New RijndaelManaged
Dim Hash_AES As New MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = CipherMode.ECB
Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
Return "Error"
End Try
End Function