B4J Question B4XEncryption problem

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
I have this code in a Static code module
B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Private C As B4XCipher
    Private str1 As String = "hgt34q"
    Private str2 As String = "M5z23t"
    Private str3 As String = "N1eDs9"
    Dim bc As ByteConverter
    Private pw As String
End Sub

Sub Crypta(dato As String) As String
    pw = str1 & str2 & str3
    Return bc.StringFromBytes(C.Encrypt(bc.StringToBytes(dato, "UTF8"), pw), "UTF8")
End Sub

Sub Decrypt(dato As String) As String
    pw = str1 & str2 & str3
    Return bc.StringFromBytes(C.Decrypt(bc.StringToBytes(dato, "UTF8"), pw), "UTF8")
End Sub

It's wrong this code?
because when compiling in debugging show this error
B4X:
B4J Version: 5.90
Parsing code.    (0.10s)
Compiling code.    (0.27s)
Compiling layouts code.    (0.03s)
Organizing libraries.    (0.00s)
Compiling generated Java code.    Error
B4J line: 20
Return bc.StringFromBytes(C.Decrypt(bc.StringToBy
javac 1.8.0_121
src\b4j\Test\cryptencrypt.java:65: error: cannot access DataLengthException
if (true) return _bc.StringFromBytes(_c.Decrypt(_bc.StringToBytes(_dato,"UTF8"),_pw),"UTF8");
                                               ^
  class file for org.bouncycastle.crypto.DataLengthException not found
1 error

Error in Sub Decrypt

Thanks
 

micro

Well-Known Member
Licensed User
Longtime User
You're right I forgot.
Now compile but ther's always the error on this code line in Decryt
B4X:
Return bc.StringFromBytes(C.Decrypt(bc.StringToBytes(dato, "UTF8"), pw), "UTF8")
B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 20 (CryptEncrypt)
org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
    at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source)
    at anywheresoftware.b4x.object.B4XEncryption.Decrypt(B4XEncryption.java:73)
Thanks
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
I resolved seeing some threads, with this code
B4X:
Sub Process_Globals
    Private fx As JFX
    Private C As B4XCipher
    Private str1 As String = "hgt34q"
    Private str2 As String = "M5z23t"
    Private str3 As String = "N1eDs9"
    Dim su As StringUtils
    Dim bc As ByteConverter
    Private pw As String
End Sub

Sub Crypta(dato As String) As String
    pw = str1 & str2 & str3
    Return su.EncodeUrl(su.EncodeBase64(C.Encrypt(dato.GetBytes("UTF8"), pw)), "UTF8")
End Sub

Sub Decrypt(dato As String) As String
    pw = str1 & str2 & str3
     Dim data(),data1() As Byte
    data = su.DecodeBase64(su.DecodeUrl(dato, "UTF8"))
    data1 = C.Decrypt(data,pw)
    dato = bc.StringFromBytes(data,"UTF8")
    If dato.Contains(Chr(0)) Then dato = dato.SubString2(0, dato.IndexOf(Chr(0)))
    Return dato
End Sub

thi is the test code but in Decrypt i do not find the original string
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Dim s As String = "prova"
    Dim scd As String
    scd = CryptDecrypt.Crypta(s)
    Log(scd)
    scd = CryptDecrypt.Decrypt(scd)
    Log(scd)
End Sub

This in debug:
------------------------------------
Waiting for debugger to connect...
Program started.
7kUM2639QFBIxXDH6uN19KHOWe%2BV9F83F7cVh9OIbZTYbu1fwyKicw%3D%3D
�Eۭ�@PH�p���u���Y��_7��ӈm��n�_�"�s
 
Upvote 0
Top