Android Question b4a encryption / pkcs7 16bit keyblock

igpf

Member
Licensed User
Longtime User
hi, not sure where to post this question. but i'm trying to "AES/CBC/PKCS7Padding" signature with 16 bit block, it has to be specific, i've been looking at all the forums, but just don't seem to make it happen. obviously, i'm new to encryption methods, not to b4a. i'm trying to connect to a python server that's using specific encryption...

the app is using tcp, to send its encoded AES256, then compressed, then padded pkcs7? if that doesn't make any sense i'm sorry, i'm using the python code to try and mimic the connection...

can someone point me in the right direction please?
 

igpf

Member
Licensed User
Longtime User
@Erel -- sorry, i'll post the code shortly

@KMatle
B4X:
c.Initialize("AES/CBC/PKCS5Padding")

This is where I was leading to.. how do you know if I can try
B4X:
c.Initialize("AES/CBC/PKCS7Padding")
?

I'm using the encryption v1.1 library.

For my understanding PKCS5Padding only allow 8bit key block, while PKCS7Padding allows 255bit... I needed to match other code that uses a 16bit key block...

So is there an update for the encryption library? or is there a work-around for the key block size issue..

sorry, i'll post code up shortly...

-igpf
 
Upvote 0

igpf

Member
Licensed User
Longtime User
B4X:
Public Sub Encode(sCMD As String) As Byte()

    Dim c As Cipher
    Dim kg As KeyGenerator
    Dim bData() As Byte
    Dim sCypherKey as String

    c.Initialize("AES/CBC/PKCS7Padding")
    sCypherKey = "12345asdf##$#$"
 
    kg.Initialize("AES")
    kg.KeyFromBytes(sCypherKey.GetBytes("utf8"))
 
    bData = c.Encrypt(sCMD.GetBytes("utf8"), kg.Key, False)
 
    Return bData
 
End Sub

there it is...
when i get to c.encrypt(cmd...) i get ...
upload_2018-10-2_2-37-0.png


any thoughts???
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
any thoughts???
You should post error messages as text.

The key should be 16 bytes long:
B4X:
sCypherKey = "12345asdf##$#$"
Dim cc() As Byte = sCypherKey.GetBytes("UTF8")
Dim keyinput(16) As Byte
Bit.ArrayCopy(cc, 0, keyinput, 0, cc.Length)
kg.Initialize("AES")
kg.KeyFromBytes(keyinput)
 
Upvote 0

igpf

Member
Licensed User
Longtime User
You should post error messages as text.

The key should be 16 bytes long:
B4X:
sCypherKey = "12345asdf##$#$"
Dim cc() As Byte = sCypherKey.GetBytes("UTF8")
Dim keyinput(16) As Byte
Bit.ArrayCopy(cc, 0, keyinput, 0, cc.Length)
kg.Initialize("AES")
kg.KeyFromBytes(keyinput)

just wanted to update, this does work. but i had found my answer.
B4X:
Dim passHash() As Byte
Dim md As MessageDigest
passHash = md.GetMessageDigest(sCypherKey.GetBytes("utf8"),"MD5")
kg.KeyFromBytes(passHash)

thanks for all the help.
 
Upvote 0
Top