B4J Question decryption from mysql AES

le_toubib

Active Member
Licensed User
Longtime User
hi all
in mysql , i encrypt the field name1 into the field name :
update `persons` set `Name` = TO_BASE64(AES_ENCRYPT(`Name1`,'pwd'))
that is working fine decrypting :
select CONVERT(AES_DECRYPT(FROM_BASE64(`Name`),'pwd') USING utf8) as n from persons where `FileNo` = 3

but in b4j :
when i use encryption library with the code :
B4X:
Sub decrypt(MeText As String) As String
    Dim kg As KeyGenerator
    Dim su As StringUtils
    Dim Cipher As Cipher
    Dim iv(0) As Byte
        iv = Array As Byte(1,1, 2,2, 3,3, 4,4, 5,5, 6,6, 7,7, 8,8) ' 16 bytes for AES
    Dim byt() As Byte = su.DecodeBase64(MeText)
    Dim k() As Byte = "pwd".GetBytes("UTF8")
        kg.Initialize("AES")
        kg.KeyFromBytes(k)       
        Cipher.Initialize("AES/CBC/PKCS5Padding") 
        Cipher.InitialisationVector = iv
    Dim bytEn() As Byte = Cipher.Decrypt(byt,kg.key,False)
    Return BytesToString(bytEn,0,bytEn.Length,"UTF-8")
End Sub

i get the following error at the line : Dim bytEn() As Byte = Cipher.Decrypt(byt,kg.key,False)
Error occurred on line: 2113 (Codes)
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:989)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
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.codes._decrypt(codes.java:2039)
at b4j.example.main._getone1(main.java:14919)
at b4j.example.main._jobdone(main.java:18213)
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:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:462)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:442)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:516)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
 

le_toubib

Active Member
Licensed User
Longtime User
Thanks
I resorted to that at the end ...
I could manually encrypt already available data in b4x ..
Load data to server ... and decrypt it back in b4x ...
BUT ..
Using my b4j fontend to post user entered data to server caused an : incomplete last Block error .
I googled it ... and it's provably bcoz base64 causing + characters that were not sends correctly through php ...
I'm trying Hex instead of base64 ... and it seems to be working fine so far...
I'll post the codes when I get home
 
Last edited:
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
B4X:
Sub AES_Encrypt(s As String) As String
        Dim Cipher As B4XCipher
        Dim b() As Byte = Cipher.Encrypt(s.GetBytes("UTF8"), "pwd")   
         Dim bc As ByteConverter
    Return bc.HexFromBytes(b)   
End Sub


Sub AES_Decrypt(s As String) As String
dim ss as string
if s <> "" then
    Dim bc As ByteConverter
    Dim b() As Byte =bc.HexToBytes(s)
        Dim Cipher As B4XCipher
        Dim bb() As Byte = Cipher.Decrypt(b, "pwd")
        Dim ss As String = BytesToString(bb, 0, bb.Length, "UTF8")
end if
    Return ss
End Sub

the above code worked fine ..
i had to encrypt sensitive data into a local csv then uploaded it data to corresponding fields (17000+ records) took 8 minutes to encrypt.
then data are encrypted/decrypted as the user add/retreive data in real time .

using base64 genrated an error :
“last block incomplete in decryption”

Use a BLOB field instead.
couldnt fetch blobs from php ( my lack of php knowledge)
couldnt use JRDC since the columns names are dynamically read . so i dont know them at design time.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I don't think that the issue is related to the base 64 encoding
Sidenote:

i´m actually wrapping an Encryption library. This libs does include two different "Allowed Chars-Sets" for Base64 En-Decoding. One normal and a "WEBsafe" Variant of it. Maybe this is exactly the case here?

Decode lookup table for the "web safe" variant (RFC 3548 sec. 4) where - and _ replace + and /.

B4X:
        /**
         * Lookup table for turning Base64 alphabet positions (6 bits)
         * into output bytes.
         */
        private static final byte ENCODE[] = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
        };

        /**
         * Lookup table for turning Base64 alphabet positions (6 bits)
         * into output bytes.
         */
        private static final byte ENCODE_WEBSAFE[] = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
        };
 
Upvote 0
Top