B4J Question Decrypt AES 128 CBC

darabon

Active Member
My data is GE3e8Qhyjk6rPiNuUJpDy/hHaCwcKj13fA9tN+ICYQM=
The data is encrypted with AES 128 CBC with 1234567890123456 key without IV
I cannot decrypt that with online sites
But cannot decrypt that in B4j
My code is:
B4X:
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim decrypted() As Byte
    Dim BConv As ByteConverter
 
    decrypted = b
 
    Dim Key As String = "1234567890123456" ' this is the same key that encrypted the string that was sent

    kg.Initialize("AES")
    kg.KeyFromBytes(Key.GetBytes("UTF8"))
   
    C.Initialize("AES/CBC/NoPadding") ' not 100% sure on the NoPadding part

    decrypted = C.Decrypt(decrypted, kg.Key, False)
 
Last edited:

agraham

Expert
Licensed User
Longtime User
CBC encryption mode needs an initialisation vector which you don't have so I am doubting your description of how it is encrypted. Try

C.Initialize("AES/ECB/NoPadding")


This does decrypt to a UTF8 string as follows, the first part of which looks plausible.

2114019028,1,ev_]��b�^�.3~@�S�
 
Upvote 0

teddybear

Well-Known Member
Licensed User
My data is GE3e8Qhyjk6rPiNuUJpDy/hHaCwcKj13fA9tN+ICYQM=
The data is encrypted with AES 128 CBC with 1234567890123456 key without IV
I cannot decrypt that with online sites
But cannot decrypt that in B4j
The original string is "2114019028,1,ev_", it can be encrypted with AES 128 CBC, padding mode is PKCS5Padding, as agraham said #5, it is not without IV, you need an initialisation vector , it is 16 bytes 0.
B4X:
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim IV(16) As Byte
    For i = 0 To 15
        IV(i) = 0
    Next
    kg.Initialize("AES")
    kg.KeyFromBytes("1234567890123456".GetBytes("utf8"))
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IV
    Dim bc As ByteConverter
    Dim b64 As Base64
    Dim datas() As Byte=b64.DecodeStoB("GE3e8Qhyjk6rPiNuUJpDy/hHaCwcKj13fA9tN+ICYQM=")
    datas = C.Decrypt(datas, kg.Key, True)
    Log(bc.StringFromBytes(datas,"utf8")) '2114019028,1,ev_

It's also encrypted or decrypted by online sites
 
Last edited:
Upvote 0

darabon

Active Member
The original string is "2114019028,1,ev_", it can be encrypted with AES 128 CBC, padding mode is PKCS5Padding, as agraham said #5, it is not without IV, you need an initialisation vector , it is 16 bytes 0.
B4X:
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim IV(16) As Byte
    For i = 0 To 15
        IV(i) = 0
    Next
    kg.Initialize("AES")
    Dim pwd() As Byte="1234567890123456".GetBytes("utf8")
    kg.KeyFromBytes(pwd)
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IV
    Dim bc As ByteConverter
    Dim b64 As Base64
    Dim datas() As Byte=b64.DecodeStoB("GE3e8Qhyjk6rPiNuUJpDy/hHaCwcKj13fA9tN+ICYQM=")
    datas = C.Decrypt(datas, kg.Key, True)
    Log(bc.StringFromBytes(datas,"utf8")) '2114019028,1,ev_

It's also encrypted or decrypted by online sites
OMG Thanks
I will test that
 
Upvote 0
Top