B4J Question KeyValueStore2 encrypt problem

asubias

Member
Licensed User
Hi, I get this error with the function getEncrypted

B4X:
javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
The stored data is an IP address

Thank you in advance :)
 

asubias

Member
Licensed User
Please post a small example with the code that inserts the data and the code that extracts it.

Insert data:
B4X:
kvs.PutEncrypted("data_db", server_txt.Text, "DBserver")
    kvs.PutEncrypted("data_db", port_txt.Text, "DBport")
    kvs.PutEncrypted("data_db", DBname_txt.Text, "DBname")
    kvs.PutEncrypted("data_db", user_txt.Text, "DBuser")
    kvs.PutEncrypted("data_db", pass_txt.Text, "DBpass")

Extract data:
B4X:
If kvs.ContainsKey("data_db") Then
        mySQLConfig.MYSQLIP = kvs.GetEncrypted("data_db", "DBserver")
        mySQLConfig.MYSQLPORT = kvs.GetEncrypted("data_db", "DBport")
        mySQLConfig.MYSQLDBNAME = kvs.GetEncrypted("data_db", "DBname")
        mySQLConfig.MYSQLUSER = kvs.GetEncrypted("data_db", "DBuser")
        mySQLConfig.MYSQLIP = kvs.GetEncrypted("data_db", "DBpass")
    End If

Data type:
B4X:
Dim MYSQLIP = "192.168.XX.XXX" As String
    Dim MYSQLDBNAME = "name"  As String
    Dim MYSQLPORT = "1234"  As String
    Dim MYSQLUSER = "user"  As String
    Dim MySQLPASS = "password1"  As String
 
Upvote 0

asubias

Member
Licensed User
We cannot help you with this code. You need to create a small example and upload it.

Ok, I made a simple test. The fist run, press the "Write data" button. It must log "data written".
Then close the app and reopen. If do the same as my phone, you will get this error:
B4X:
Error occurred on line: 49 (Main)
javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
    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:2056)
    at anywheresoftware.b4a.object.B4XEncryption.Decrypt(B4XEncryption.java:47)
    at b4a.example3.keyvaluestore._getencrypted(keyvaluestore.java:139)
    at b4a.example.main._loadsaves(main.java:412)
    at b4a.example.main._activity_create(main.java:393)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.main.afterFirstLayout(main.java:104)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Regards,
Alberto
 

Attachments

  • kvs test.zip
    9 KB · Views: 197
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It must log "data written".
it does.

Please note that you are using the same key for all the values.
B4X:
    kvs.PutEncrypted("data_db", "192.168.1.31", "DBserver")
    kvs.PutEncrypted("data_db", 1234, "DBport")
    kvs.PutEncrypted("data_db", "kvs_test", "DBname")
    kvs.PutEncrypted("data_db", "admin", "DBuser")
    kvs.PutEncrypted("data_db", "pw1234", "DBpass")
Only ONE key is used. So at the end the key data_db contains the encrypted pw1234.
When you start your app
B4X:
        Log(kvs.GetEncrypted("data_db", "DBserver"))
will read the key data_db with the passwort for the Server. BUT the Data inside the key is the password! But you are using the server password to decrypt. It is the WRONG password to decrypt the pw1234!


Solution:
Use different KEYnames for the different values you want to store.

B4X:
Sub loadSaves
    If kvs.ContainsKey("data_db") Then
        Log("exist data")
        Log(kvs.GetEncrypted("data_db_server", "DBserver"))
        Log(kvs.GetEncrypted("data_db_port", "DBport"))
        Log(kvs.GetEncrypted("data_db_name", "DBname"))
        Log(kvs.GetEncrypted("data_db_user", "DBuser"))
        Log(kvs.GetEncrypted("data_db_pass", "DBpass"))
    End If
End Sub

Sub writeSaves
    kvs.PutEncrypted("data_db_server", "192.168.1.31", "DBserver")
    kvs.PutEncrypted("data_db_port", 1234, "DBport")
    kvs.PutEncrypted("data_db_name", "kvs_test", "DBname")
    kvs.PutEncrypted("data_db_user", "admin", "DBuser")
    kvs.PutEncrypted("data_db_pass", "pw1234", "DBpass")
    Log("data written")
End Sub

OR use a Map containing all values and save/load this map.

B4X:
Sub loadSaves
    If kvs.ContainsKey("data_db") Then
        Log("exist data")
        Dim m As Map = kvs.GetEncrypted("data_db", "SecurityPW")
        Log(m)
    End If
End Sub

Sub writeSaves
    Dim m As Map = CreateMap("Server": "192.168.1.31", "Port": 1234,"Name":"kvs_test","User":"admin","Passord":"pw1234")
    kvs.PutEncrypted("data_db", m, "SecurityPW")
    Log("data written")
End Sub
 
Last edited:
Upvote 0

asubias

Member
Licensed User
Only ONE key is used. So at the end the key data_db contains the encrypted pw1234.
When you start your app
B4X:
        Log(kvs.GetEncrypted("data_db", "DBserver"))
will read the key data_db with the passwort for the Server. BUT the Data inside the key is the password! But you are using the server password to decrypt. It is the WRONG password to decrypt the pw1234!
Ohh. Thank you very much Manfred. :)
I thought that the password (last value of putEncrypted) was the "key" to get it and the key parameter was the name of the encrypted file... I didn't understand correctly.
So, password is the string used to encrypt the value to store??

Regards,
Alberto
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
password is the string used to encrypt the value to store?
yes.
and the key parameter was the name of the encrypted file
No. KVS is using ONE Sqlite-Database file which name you set in initialization
B4X:
kvs.Initialize(File.DirInternal, "internal_data")
the file internal_data is the database file where ALL is saved in or loaded from.
 
Upvote 0

asubias

Member
Licensed User
yes.

No. KVS is using ONE Sqlite-Database file which name you set in initialization
B4X:
kvs.Initialize(File.DirInternal, "internal_data")
the file internal_data is the database file where ALL is saved in or loaded from.
Ok, thank you again.

Best regards,
Alberto
 
Upvote 0
Top