Android Question [solved] EncryptText String in Map

MarkusR

Well-Known Member
Licensed User
hello,
i try to Encrypt a string into a map Password key.
i will use WriteMap for saving
then i will use ReadMap and Decrypt the key Password back into original

but decrypt did not work and run into error^^

code modul start with Test
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim SettingsMap As Map
    Dim FileName As String
    Dim Password As String

End Sub

Sub Test
   
    Log("Test")
   
    SettingsInit
   
    SettingsLoad

    Log( SettingsMap.Get("User"))
    Log( DecryptText(SettingsMap.Get("Password")))
       
    SettingsMap.Put("User","Markus")
    SettingsMap.Put("Password",EncryptText("MeinPassword"))
   
    SettingsSave
   
    SettingsLoad
   
    Log( SettingsMap.Get("User"))
    Log( DecryptText(SettingsMap.Get("Password")))
   
End Sub

Sub SettingsInit
   
    Log("SettingsInit")
   

    FileName = "BinPackingSettings.dat"

    Password = "123"

    SettingsMap.Initialize
    SettingsMap.Clear
    SettingsMap.Put("User","")
    SettingsMap.Put("Password","")
 
End Sub

Sub SettingsSave() As Boolean

    Log("SettingsSave")

    File.WriteMap(File.DirInternal,FileName,SettingsMap)
 
    Log("Saved")
 
    Return True
 
End Sub

Sub SettingsLoad()

    Log("SettingsLoad")

    If File.Exists(File.DirInternal,FileName)=True Then

        SettingsMap = File.ReadMap(File.DirInternal,FileName)

        Log("Loaded")

    Else
        ToastMessageShow("new settings",False)
    End If
 
End Sub

'verschlüsseln
Sub EncryptText(text As String) As String

    Log("EncryptText " & text)

    Dim c As B4XCipher
    Dim e() As Byte
    e = c.Encrypt(text.GetBytes("utf8"), Password)

    Return BytesToString(e,0,e.Length,"UTF-8")

End Sub

'entschlüsseln
Sub DecryptText(text As String) As String

    Log("DecryptText " & text)

    'Try
        Dim c As B4XCipher
        Dim a() As Byte = text.GetBytes("utf8")
        If a.Length=0 Then Return ""
        Dim b() As Byte = c.Decrypt( a, Password)
        Return BytesToString(b, 0, b.Length, "UTF-8")
    'Catch
    '        Log(LastException)
    '        Return ""
    '    End Try

End Sub
Snap_2018.05.22_20h42m20s_001_.png
 
Last edited:

MarkusR

Well-Known Member
Licensed User
no problem yet
Test
SettingsInit
SettingsLoad
Loaded
Markus
DecryptText Δ[n�H�`�3�{up%%�,���W��=�ַ31WE]h
Error occurred on line: 102 (TestModul)
javax.crypto.IllegalBlockSizeException: error:1e06b07b:Cipher functions:EVP_DecryptFinal_ex:WRONG_FINAL_BLOCK_LENGTH
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:568)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:350)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at anywheresoftware.b4a.object.B4XEncryption.Decrypt(B4XEncryption.java:47)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at com.rauch.test.testmodul._test(testmodul.java:24)
at com.rauch.test.main._activity_create(main.java:406)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at com.rauch.test.main.afterFirstLayout(main.java:104)
at com.rauch.test.main.access$000(main.java:17)
at com.rauch.test.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 

KZero

Active Member
Licensed User
Use Encryption Library
B4X:
Sub EncryptText(text As String) As String
    Log("EncryptText " & text)
    Dim c As B4XCipher
    Dim e() As Byte
    e = c.Encrypt(text.GetBytes("utf8"), Password)
    Dim B64 As Base64
    Return B64.EncodeBtoS(e,0,e.Length)
End Sub

Sub DecryptText(text As String) As String
    Log("DecryptText " & text)
    If text.Length=0 Then Return ""
    Dim B64 As Base64
    Dim c As B4XCipher
    Dim a() As Byte = B64.DecodeStoB(text)
    Dim b() As Byte = c.Decrypt( a, Password)
    Return BytesToString(b, 0, b.Length, "UTF-8")
End Sub
 

MarkusR

Well-Known Member
Licensed User
@KZero
thank you, in the same time i got this :)
the idea with base 64 works

B4X:
'verschlüsseln
Sub EncryptText(text As String) As String

    Log("EncryptText " & text)

    Dim c As B4XCipher

    Dim e() As Byte
    e = c.Encrypt(text.GetBytes("utf8"), Password)
    
    Dim su As StringUtils
    
    Return su.EncodeBase64(e)

End Sub

'entschlüsseln
Sub DecryptText(textb64 As String) As String

    Log("DecryptText " & textb64)

    'Try
        Dim c As B4XCipher
        Dim su As StringUtils
    Dim a() As Byte = su.DecodeBase64(textb64)
        If a.Length=0 Then Return ""
        Dim b() As Byte = c.Decrypt( a, Password)
        
        Return BytesToString(b, 0, b.Length, "UTF-8")
    'Catch
    '        Log(LastException)
    '        Return ""
    '    End Try

End Sub
 
Top