Android Question decrypt datas with AES/CBC system

sirjo66

Well-Known Member
Licensed User
Longtime User
Hello,
I need to decrypt data with AES/CBC system,
here is my VB.NET routine that I need to convert in B4A:

B4X:
  Public Function AES_Decrypt(ByVal input As String, ByVal IV As String, ByVal pass As String) As String

  Dim inputB = Str2Byte(input)  ' convert input data from string di byte array
  Dim passB = Str2Byte(pass)   ' convert password from string to byte array
  Dim IVb = Str2Byte(IV)   ' convert IV from string to byte array

' now inputB, passB and IVb are byte array

  Dim AES As New System.Security.Cryptography.RijndaelManaged
  AES.Padding = System.Security.Cryptography.PaddingMode.PKCS7
  AES.Mode = System.Security.Cryptography.CipherMode.CBC
  AES.KeySize = 128
  AES.BlockSize = 128
  AES.IV = IVb
  AES.Key = passB

  Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
  Dim decryB = DESDecrypter.TransformFinalBlock(inputB, 0, inputB.Length)

  Return Encoding.Default.GetString(decryB)  ' convert byte array to string

  End Function

How can do it in B4A ??

Many thanks

Sergio
 

sirjo66

Well-Known Member
Licensed User
Longtime User
IT WORKS !!!

Many thanks

Sergio

P.S.: the code works right because I know that decrypted datas are ALWAYS string, so I need to convert to string at the end of the routine

B4X:
Sub AES_Decrypt(input As String, IV As String, pass As String) As String

   Dim inputB() As Byte = Str2Byte(input)
   Dim passB() As Byte = Str2Byte(pass)
   Dim IVb() As Byte = Str2Byte(IV)
   
   Dim kg As KeyGenerator
   Dim C As Cipher
 
   kg.Initialize("AES")
   kg.KeyFromBytes(passB)
 
   C.Initialize("AES/CBC/PKCS7Padding")
   C.InitialisationVector = IVb
 
   Dim datas() As Byte = C.Decrypt(inputb, kg.Key, True)
 
   Return BytesToString(datas, 0, datas.Length, "UTF8")

End Sub
 
Upvote 0
Top