Android Question [SOLVED] VB.NET and B4A AES Encryption Problem

Discussion in 'Android Questions' started by mmieher, Jan 12, 2019.

  1. mmieher

    mmieher Active Member Licensed User

    Still having trouble with encryption. I need to be able to encrypt/decrypt the same string in both B4A and VB.NET and get the same result.
    I am using the same input string and key in the code below but getting two different results. I don't know enough about encryption to understand why.

    B4A:
    Code:
    Sub AESEncrypt(dataToEncrypt As String, MyAESPW As StringAs 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/PKCS5Padding")
        encrypted = C.Encrypt(dataToEncrypt.GetBytes(
    "UTF8"), kg.Key, False)
        
    Return SU.EncodeBase64(encrypted)
      
    End Sub
    VB.NET
    Code:
    Public Function AES_Encrypt(ByVal input As String, ByVal pass As StringAs String
            
    Dim AES As New RijndaelManaged
            
    Dim Hash_AES As New MD5CryptoServiceProvider
            
    Dim encrypted As String = ""
            
    Try
                
    Dim hash(31As Byte
                
    Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass))
                
    Array.Copy(temp, 0, hash, 016)
                
    Array.Copy(temp, 0, hash, 1516)
                AES.Key = hash
                AES.Mode = CipherMode.ECB                                    
    ''    I have tried every option here
                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
                Debug.Print(ex.Message)
                
    Return "Error"
            
    End Try
        
    End Function
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Start simple and remove the MD5 / hash code. Start with a 16 bytes key.
     
  3. mmieher

    mmieher Active Member Licensed User

    Erel, what should I replace "MD5" parameter with? I have to put something.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    What happens if you change this code:
    Code:
    Array.Copy(temp, 0, hash, 016)
                
    Array.Copy(temp, 0, hash, 1516)
                AES.Key = hash
    To:
    Code:
    AES.Key = temp
    ?

    What is the size of temp?
     
  5. mmieher

    mmieher Active Member Licensed User

    Erel, changing code as specified above produces a different result, but VB and B4A still do not match. The length of temp is 16.

    VB: VciXMq2Bki7m6gM2lB4OZg==

    B4A: cHY48ab60xfup5cDLxYxng==
     
  6. mmieher

    mmieher Active Member Licensed User

    Disregard last post! I made an error when I changed the code. Your suggestion made it work! Thank you!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice