B4J Question AES128 ECB Decrypt

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to decrypt a message using AES128 in ECB mode, and not CBC mode.

The data I am trying to decrypt is sent to my B4J as AES-128 in ECB mode, and that data I have put into a string called 'dataToDecrypt' in my code below.

When I run the following code I get the following error:

Waiting for debugger to connect...
Program started.
Error occurred on line: 83 (Main)
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1016)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:960)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.doFinal(CipherWrapper.java:140)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.Decrypt(CipherWrapper.java:150)
at b4j.example.main._decrypt(main.java:284)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.main(main.java:29)

Line 83 is: decrypted = C.Decrypt(decrypted, kg.Key, False)

B4X:
Dim dataToDecrypt As String = "�q�!DAZ��R��:f/�Ŝ��h�U�0h�V��ƅHw��s�Č)�o�yZ��%v��_,"
   
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim decrypted() As Byte
    Dim BConv As ByteConverter
   
    decrypted = dataToDecrypt.GetBytes("UTF8")
 
    Dim Key As String = "0123456789ABCDEF" ' this is the same key that encrypted the string that was sent

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

    decrypted = C.Decrypt(decrypted, kg.Key, False)

    Log("Decrypt = " & BConv.StringFromBytes(decrypted, "UTF8"))

Anyone know where I have gone wrong ?
 
Top