iOS Question [SOLVED] B4i KeyGenerator Problem

mmieher

Active Member
Licensed User
Porting B4a to B4i...

I am including iEncryption 1.02 in my project. The line "Dim kg as KeyGenerator" says "Unknown Type". I see lots of examples where other people are successfully using the same line. What could be wrong?
 

mmieher

Active Member
Licensed User
Thank you, Erel. Getting closer. Trying to get VB.NET, B4A and B4I to all encrypt nicely together, but getting a different result.

B4A code:
B4X:
Public Sub AESEncrypt(dataToEncrypt As String, MyAESPW As String) As String
  
    Dim SU As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim encrypted() As Byte
    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(MyAESPW.GetBytes("UTF8"), "MD5"))
      
    C.Initialize("AES/ECB/PKCS7Padding")
    encrypted = C.Encrypt(dataToEncrypt.GetBytes("UTF8"), kg.Key, False)
    Return SU.EncodeBase64(encrypted)
  
End Sub
Result = cHY48ab60xfup5cDLxYxng==

B4i CODE:
B4X:
Public Sub AESEncrypt(dataToEncrypt As String, MyAESPW As String) As String
  
    Dim SU As StringUtils
    Dim C As Cipher
    Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding,C.OPTION_ECBMode)
    Dim bData() As Byte = C.Encrypt2(dataToEncrypt.GetBytes("utf8"),MyAESPW.GetBytes("utf8"),"AES",Null,iOption)
  
    Return SU.EncodeBase64(bData)
  
End Sub
Result = xUUDxJpYP2KhIak7qA8PvQ==
 
Upvote 0

mmieher

Active Member
Licensed User
Thanks. That did the trick. For anyone else that it might help:
B4X:
Public Sub AESEncrypt(dataToEncrypt As String, MyAESPW As String) As String
   
    Dim SU As StringUtils
    Dim md As MessageDigest
    Dim C As Cipher

    Dim bMD() As Byte = md.GetMessageDigest(MyAESPW.GetBytes("UTF8"),"MD5")
    Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding,C.OPTION_ECBMode)
    Dim bData() As Byte = C.Encrypt2(dataToEncrypt.GetBytes("utf8"),bMD,"AES",Null,iOption)
   
    Return SU.EncodeBase64(bData)
   
End Sub
 
Upvote 0

Alex_197

Active Member
Licensed User
Thanks. That did the trick. For anyone else that it might help:
B4X:
Public Sub AESEncrypt(dataToEncrypt As String, MyAESPW As String) As String
 
    Dim SU As StringUtils
    Dim md As MessageDigest
    Dim C As Cipher

    Dim bMD() As Byte = md.GetMessageDigest(MyAESPW.GetBytes("UTF8"),"MD5")
    Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding,C.OPTION_ECBMode)
    Dim bData() As Byte = C.Encrypt2(dataToEncrypt.GetBytes("utf8"),bMD,"AES",Null,iOption)
 
    Return SU.EncodeBase64(bData)
 
End Sub

Did you try to create VB.NET code do decrypt it? And may I ask you - MyASPW is a just a some password string like abc#$%Aqq?

Thanks
 
Upvote 0

mmieher

Active Member
Licensed User
Try this ...

VB.NET code on the other end.

B4X:
 Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New RijndaelManaged
        Dim Hash_AES As New MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass))
            '   Array.Copy(temp, 0, hash, 0, 16)
            '  Array.Copy(temp, 0, hash, 15, 16)
            ' AES.Key = hash
            AES.Key = temp  '   Erel
            AES.Mode = CipherMode.ECB
            AES.Padding = PaddingMode.PKCS7
            Dim DESDecrypter As ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return decrypted
        Catch ex As Exception
            Return "Error"
        End Try
    End Function
 
Upvote 0

Alex_197

Active Member
Licensed User
Try this ...

VB.NET code on the other end.

B4X:
 Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New RijndaelManaged
        Dim Hash_AES As New MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass))
            '   Array.Copy(temp, 0, hash, 0, 16)
            '  Array.Copy(temp, 0, hash, 15, 16)
            ' AES.Key = hash
            AES.Key = temp  '   Erel
            AES.Mode = CipherMode.ECB
            AES.Padding = PaddingMode.PKCS7
            Dim DESDecrypter As ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return decrypted
        Catch ex As Exception
            Return "Error"
        End Try
    End Function
Thank you:)
It works.

Did you try to encrypt it in VB.NET?
 
Upvote 0
Top