Hi,
I am trying to encrypt a message using AES128 ECB.
I ended up working out how to decrypt the message, but can't work out how to encrypt the message back again.
I have tried used the following code..
(not sure if this is correct or not)
It seems to log:
textBytes.Length = 49
30303030373534432F2F3030343039443237353538442F2F30352E30332E31302F2F30322E30302E34342F2F48656C6C6F
textEncryptedBytes.Length = 64
CB517EF65D218017C806EE2D4CF03E1A7859FD08E48E1F1D799E9A8D8593041ED6BC07D879092756A99BE059CA98BFEBAB2E5093707E61AC225B3B5C1B3F8D06
textLength = 49
byteBlocks * 16 = 64
textPadded.Length = 64
30303030373534432F2F3030343039443237353538442F2F30352E30332E31302F2F30322E30302E34342F2F48656C6C6F000000000000000000000000000000
bufferEncrypted: �Q~�]!���-L�>4�3l�5Vs�c���
bufferEncrypted: xY��y�����4�3l�5Vs�c���
bufferEncrypted: ּ�y 'V���Yʘ��4�3l�5Vs�c���
bufferEncrypted: E%R=���0�t���4�3l�5Vs�c���
However the final message should be:
�q�!DAZ��R��:f/�Ŝ��h�U�0h�V��ƅHw��s�Č)�o�yZ��%v��_,"
Any ideas on what I have done wrong ?
I am trying to encrypt a message using AES128 ECB.
I ended up working out how to decrypt the message, but can't work out how to encrypt the message back again.
I have tried used the following code..
(not sure if this is correct or not)
B4X:
Sub Encrypt As String
Dim Key As String = "0123456789ABCDEF" ' MDEyMzQ1Njc4OUFCQ0RFRg==
Dim bc As ByteConverter
Dim kg As KeyGenerator
Dim C As Cipher
C.Initialize("AES/ECB/PKCS5Padding")
kg.Initialize("AES")
kg.KeyFromBytes(Key.GetBytes("UTF8"))
Dim textToEncrypt As String = "0000754C//00409D27558D//05.03.10//02.00.44//Hello"
Dim textBytes() As Byte = textToEncrypt.GetBytes("ASCII")
Log($"textBytes.Length = ${textBytes.Length}"$)
Log(bc.HexFromBytes(textBytes))
Dim textEncryptedBytes() As Byte = C.Encrypt(textBytes, kg.Key, False)
Log($"textEncryptedBytes.Length = ${textEncryptedBytes.Length}"$)
Log(bc.HexFromBytes(textEncryptedBytes))
Dim textLength As Int = textBytes.Length
Log($"textLength = ${textLength}"$)
Dim byteBlocks As Int = textLength / 16
If (textLength Mod 16) > 0 Then byteBlocks = byteBlocks + 1
Log($"byteBlocks * 16 = ${byteBlocks *16}"$)
Dim textPadded(byteBlocks * 16) As Byte
bc.ArrayCopy(textBytes, 0, textPadded, 0, textBytes.Length)
Log($"textPadded.Length = ${textPadded.Length}"$)
Log(bc.HexFromBytes(textPadded))
For x = 0 To byteBlocks - 1
Dim buffer(16) As Byte
bc.ArrayCopy(textPadded, x * 16, buffer, 0, 16)
Dim bufferEncrypted() As Byte = C.Encrypt(buffer, kg.Key, False)
Log($"bufferEncrypted: ${bc.StringFromBytes(bufferEncrypted,"UTF-8")}"$)
Next
End Sub
It seems to log:
textBytes.Length = 49
30303030373534432F2F3030343039443237353538442F2F30352E30332E31302F2F30322E30302E34342F2F48656C6C6F
textEncryptedBytes.Length = 64
CB517EF65D218017C806EE2D4CF03E1A7859FD08E48E1F1D799E9A8D8593041ED6BC07D879092756A99BE059CA98BFEBAB2E5093707E61AC225B3B5C1B3F8D06
textLength = 49
byteBlocks * 16 = 64
textPadded.Length = 64
30303030373534432F2F3030343039443237353538442F2F30352E30332E31302F2F30322E30302E34342F2F48656C6C6F000000000000000000000000000000
bufferEncrypted: �Q~�]!���-L�>4�3l�5Vs�c���
bufferEncrypted: xY��y�����4�3l�5Vs�c���
bufferEncrypted: ּ�y 'V���Yʘ��4�3l�5Vs�c���
bufferEncrypted: E%R=���0�t���4�3l�5Vs�c���
However the final message should be:
�q�!DAZ��R��:f/�Ŝ��h�U�0h�V��ƅHw��s�Č)�o�yZ��%v��_,"
Any ideas on what I have done wrong ?