Android Question aes encryption with key and iv => decryption server side

ALBRECHT

Active Member
Licensed User
Hello,

After looking for some days the right way to decrypt and check via my API (in ASP vbscript) from a remote server, a string sent by poststring from my APP and encrypted with AES 128bits with key and iv specific ,
I can share my result with you :


App Side Sub :
B4X:
Sub EncryptTextCipher(text As String, akey As String, aniv As String) As String
    Dim data() As Byte
    Dim iv() As Byte
    Dim su As StringUtils
    Dim bc As ByteConverter

    Dim thestr As String = text
  
    Dim kg As KeyGenerator
    kg.Initialize("AES")
  
    Dim keystr As String = akey 'pass there your 16byte key
    kg.KeyFromBytes(bc.StringToBytes(keystr, "UTF8"))  
    Dim ivstr As String = aniv  'pass there your 16byte iv
    iv = bc.StringToBytes(ivstr, "UTF8")
  
    Dim c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding")
    c.InitialisationVector = iv

    data = bc.StringToBytes(thestr, "UTF8")
    data = c.Encrypt(data, kg.key, True)
    Return su.EncodeBase64(data)
End Sub
and server side in ASP (vbscript) :
B4X:
Function decryptAes(str)
  set obj= CreateObject("System.Security.Cryptography.RijndaelManaged")
  set utf8 = CreateObject("System.Text.UTF8Encoding")
  ekey = "****************" 'pass there the same 16byte key
  eiv = "****************" 'pass there the same 16byte iv
  obj.BlockSize = 128
  obj.Key = utf8.GetBytes_4(ekey)
  obj.IV = utf8.GetBytes_4(eiv)
  obj.Padding = 5
  set decryptor=obj.CreateDecryptor()
  ChaDecypt = Base64ToByte(str)
  byted=decryptor.TransformFinalBlock((ChaDecypt),0,lenb(ChaDecypt))
  ChaDecypt = utf8.getString((byted))
  decryptAes = ChaDecypt
end function

Function Base64ToByte(ByVal B64Str)
  Dim MsXML, MsNode
  Set MsXML = CreateObject("Msxml2.DOMDocument.3.0")
  Set MsNode = MsXML.CreateElement("base64")
  MsNode.dataType = "bin.base64"
  MsNode.text = B64Str
  Base64ToByte = MsNode.nodeTypedValue
  Set MsNode = Nothing
  Set MsXML = Nothing
End Function
Im using Encryption library instead B4XEncryption library because
i need to use the good key and iv string Server Side for checking

if it may interest someone or an idea to improve the process...
 
Last edited:
Top