Android Question Encrypt on Android - Decrypt on .Net

Discussion in 'Android Questions' started by pereskjo, May 22, 2018.

  1. pereskjo

    pereskjo Member Licensed User

    Hi, I need to send a encrypted String from Android Phone to Windows computer.
    So I need a B4A Encrypt , and C# / vb.net Decrypt code.
    I have tried various DES, 3DES libraries.
    So if any one of you have got this right, please post the code below.
     
  2. KZero

    KZero Active Member Licensed User

  3. pereskjo

    pereskjo Member Licensed User

    Not any luck yet. Tried Rijandel code in link, but the B4A code generate different output each time i input "hello world" ??
    added
    Dim bc As ByteConverter
    Return bc.StringFromBytes(encrypted,"UTF-8")
    is that wrong?

    B4A:
    Code:
    Sub Button1_Click
        
    ToastMessageShow("Encrypted = " & Encrypt("hello world"),False)
    End Sub

    Sub Encrypt(dataToEncrypt As StringAs String
        
    Dim kg As KeyGenerator
        
    Dim C As Cipher
        
    Dim encrypted() As Byte
     
        kg.Initialize(
    "AES")
        kg.KeyFromBytes(
    "5TGB&YHN7UJM(IK<".GetBytes("UTF8"))
      
        C.Initialize(
    "AES/CBC/PKCS7Padding")
          C.InitialisationVector = 
    "!QAZ2WSX#EDC4RFV".GetBytes("UTF8")
        encrypted = C.Encrypt(dataToEncrypt.GetBytes(
    "UTF8"), kg.Key, False)
        
    Dim bc As ByteConverter
        
    Return bc.StringFromBytes(encrypted,"UTF-8")
    End Sub
    Please help if any of you got B4A -> .Net encrypd decrypt working.
     
    Last edited: May 23, 2018
  4. Jeffrey Cameron

    Jeffrey Cameron Active Member Licensed User

    I usually hex encode the bytes but you could base-64 or post the bytes directly. I use this for URI parameters, for example, .NET CODE:
    Code:
    Public Shared Function AESDecrypt(ByVal HexString As StringAs String
            
    Dim poBytes As New List(Of Byte)
            
    Dim piIndex As Integer
            
    Dim pyResult() As Byte

            
    Try
                
    For piIndex = 0 To HexString.Length - 1 Step 2
                    
    Dim psChars As String
                    psChars = HexString.Substring(piIndex, 
    2)
                    poBytes.Add(Convert.ToInt32(psChars, 
    16))
                
    Next

                
    ' decrypt it
                Dim poAES As New System.Security.Cryptography.AesManaged

                poAES.IV = System.Text.UTF8Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings(
    "AesIV"))
                poAES.Key = System.Text.UTF8Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings(
    "AesKey"))
                pyResult = poAES.CreateDecryptor().TransformFinalBlock(poBytes.ToArray, 
    0, poBytes.Count)
                poAES.Clear()

                
    Return System.Text.UTF8Encoding.UTF8.GetString(pyResult)

            
    Catch ex As Exception
                clsLogError.WriteToLog(
    "WebCore.AESDecrypt""RTE attempting AES decrypt of string: " & ex.Message & "<br />Input: [&nbsp;<b>" & HexString & "</b>&nbsp;]")
                
    Return ""
            
    End Try

        
    End Function

        
    Public Shared Function AESEncrypt(ByVal PlainText As StringAs String
            
    Dim pyBytes() As Byte
            
    Dim pyResult() As Byte
            
    Dim piIndex As Integer
            
    Dim poResult As New System.Text.StringBuilder

            pyBytes = System.Text.UTF8Encoding.UTF8.GetBytes(PlainText)

            
    ' Encrypt it
            Dim poAES As New System.Security.Cryptography.AesManaged

            poAES.IV = System.Text.UTF8Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings(
    "AesIV"))
            poAES.Key = System.Text.UTF8Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings(
    "AesKey"))
            pyResult = poAES.CreateEncryptor().TransformFinalBlock(pyBytes, 
    0, pyBytes.Length)
            
    ' convert it to a HEX string
            For piIndex = 0 To pyResult.Length - 1
                poResult.Append(pyResult(piIndex).ToString(
    "X2"))
            
    Next

            
    Return poResult.ToString
        
    End Function
    B4A Code:
    Code:
    ' Note:  REQUIRES:   Encryption, B4XEncryption, ByteConverter, StringUtils libraries

    ' In Starter module:
     Public Const PART_3 As String = "16RANDOMBYTESOne"   ' command line parameter encryption
     Public Const PART_4 As String = "16RANDOMBYTESTwo"

    ' in a module:

    'Takes a plain string and returns as HEX-ENCODED string of the encrytped text.
    Public Sub AES_Encrypt(DataToEncrypt As StringAs String
        
    Dim poKG As KeyGenerator
        
    Dim poCy As Cipher
        
    Dim poBC As ByteConverter
        
    Dim poData() As Byte

        
    ' doing AES
        poCy.Initialize("AES/CBC/PKCS7Padding")
        
    ' set InitializationVector value
        poCy.InitialisationVector = poBC.StringToBytes(Starter.PART_3, "utf8")
        
    ' Generate a key
        poKG.Initialize("AES")
        poKG.KeyFromBytes(poBC.StringToBytes(Starter.PART_4, 
    "utf8"))
        
    ' encrypt the string into a byte array   
        poData = poCy.Encrypt(poBC.StringToBytes(DataToEncrypt, "utf8"), poKG.Key, True)
        
    ' convert the byte array to a HEX string and return it
        Return poBC.HexFromBytes(poData)
    End Sub

    ' Takes a HEX-ENCODED string of the encrytped text and returns the plain text.
    Public Sub AES_Decrypt(HexEncodedString As StringAs String
        
    Dim poKG As KeyGenerator
        
    Dim poCy As Cipher
        
    Dim poBC As ByteConverter
        
    Dim poData() As Byte

        poData = poBC.HexToBytes(HexEncodedString)
        
    ' doing AES
        poCy.Initialize("AES/CBC/PKCS7Padding")
        
    ' set InitializationVector value
        poCy.InitialisationVector = poBC.StringToBytes(Starter.PART_3, "utf8")
        
    ' Generate a key
        poKG.Initialize("AES")
        poKG.KeyFromBytes(poBC.StringToBytes(Starter.PART_4, 
    "utf8"))
        
    ' encrypt the string into a byte array   
        poData = poCy.Decrypt(poBC.HexToBytes(HexEncodedString), poKG.Key, True)
        
    ' Convert it back to utf8 string
        Return BytesToString(poData, 0, poData.Length, "utf8")
    End Sub
     
  5. pereskjo

    pereskjo Member Licensed User

    Awesome Jeffrey, Thanks.:)
    Just changed your ConfigurationManager.AppSettings with a 16 char long string for iv and key, and it worked.
     
    Last edited: May 24, 2018
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