Android Question VB6 function to decrypt AES-text from b4a

Michael Müller Anywhere

Member
Licensed User
Hello,
I want to decrypt a string I have encrypted before in b4a and send it to a VB6-application via TCI/IP.
This is my code:

ecnrypt a string:
Sub AES_encrypt(was As String) As String
Dim PW = "myPW1234561234567890123456789012" As String
Dim salt = "1234567890123456" As String
Dim IV = "IV98765432109876" As String   

    Return my_encrypt( was.GetBytes("UTF8"), PW.GetBytes("UTF8"), salt.GetBytes("UTF8"), IV.GetBytes("UTF8"),  True)
End Sub


Sub my_encrypt(data() As Byte, passB() As Byte, SaltB() As Byte, IVb() As Byte, ReturnB64String As Boolean) As Object
Dim su As StringUtils
Dim kg As KeyGenerator
Dim C As Cipher
 
    kg.Initialize("AES")
    kg.KeyFromBytes(passB)
 
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IVb
 
    Dim datas() As Byte = C.Encrypt(data, kg.Key, True)

    Dim SaltIVMessage(SaltB.Length + datas.Length) As Byte = AddSaltIVMessage(SaltB,IVb, datas)

    Log("Länge=" & (SaltB.Length + datas.Length + IVb.Length))
    
    If ReturnB64String Then
        Return su.EncodeBase64(SaltIVMessage)
    Else
        Return SaltIVMessage
    End If


End Sub

Private Sub AddSaltIVMessage (Salt() As Byte,IV() As Byte, Message () As Byte) As Byte()
Dim bc As ByteConverter
Dim SaltIVMessageBytes (Salt.Length + IV.Length + Message.Length) As Byte

    bc.ArrayCopy(Salt,0,SaltIVMessageBytes,0,32)
    bc.ArrayCopy(IV,0,SaltIVMessageBytes,32,16)
    bc.ArrayCopy(Message,0,SaltIVMessageBytes,48,Message.Length)

    
    Return SaltIVMessageBytes
End Sub


Does anyone know of a working function/solution for VB6 to decode the text?

I have allready tried in vain the "mdAesCtr.bas" module (over hours) from this side:

But it seams then InitialisationVector in this module this is not taken into account.

Thank you!
 

HAH

Well-Known Member
Licensed User
See this:
 
Upvote 0

Michael Müller Anywhere

Member
Licensed User
My solution for everybody who's interested:
I use the crypt-activeX-component from "weonlydo.com".
The code is simple (salt is not nessesary):

Code in the VB6 module:
Option Explicit
Dim DecryptedBlob As MemBlob
Dim EncryptedBlob As MemBlob
Dim Crypto As wodCryptCom
Public Function wod_decrypt(was As String) As String
    Crypto.LicenseKey = "xxxx-xxxx-xxxx-xxxx"
    Crypto.Optimized = False
    Crypto.SecretKey = "mysecretkey"
    Crypto.InitVector = "IV98765432109876"
    Crypto.mode = CBC
    Crypto.type = 4 'AES256
    EncryptedBlob.FromBase64 was
    Crypto.DeCrypt EncryptedBlob, DecryptedBlob
    wod_decrypt = Mid(DecryptedBlob.Text, Len(Crypto.InitVector) + 1)
End Function

I only have to remove the salt-part from the code above in my first thread:

Code in ba4:
Sub my_encrypt(data() As Byte, passB() As Byte, IVb() As Byte, ReturnB64String As Boolean) As Object
Dim su As StringUtils
Dim kg As KeyGenerator
Dim C As Cipher
 
    kg.Initialize("AES")
    kg.KeyFromBytes(passB)
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IVb
 
    Dim datas() As Byte = C.Encrypt(data, kg.Key, True)
    Dim SaltIVMessage(IVb.Length + datas.Length) As Byte = AddNoSaltIVMessage(IVb, datas)
    
    If ReturnB64String Then
        Return su.EncodeBase64(SaltIVMessage)
    Else
        Return SaltIVMessage
    End If
End Sub

Private Sub AddNoSaltIVMessage (IV() As Byte, Message () As Byte) As Byte()
Dim bc As ByteConverter
    Dim SaltIVMessageBytes (IV.Length + Message.Length) As Byte
    bc.ArrayCopy(IV,0,SaltIVMessageBytes,0,16)
    bc.ArrayCopy(Message,0,SaltIVMessageBytes,16,Message.Length)
    Return SaltIVMessageBytes
End Sub
 
Upvote 0
Top