B4J Question KeyValueStore2 encrypt problem

Discussion in 'B4J Questions' started by asubias, Jan 7, 2019.

  1. asubias

    asubias Member Licensed User

    Hi, I get this error with the function getEncrypted

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

    Thank you in advance :)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please post a small example with the code that inserts the data and the code that extracts it.
     
  3. asubias

    asubias Member Licensed User

    Insert data:
    Code:
    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:
    Code:
    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:
    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    We cannot help you with this code. You need to create a small example and upload it.
     
  5. asubias

    asubias Member Licensed User

    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:
    Code:
    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
     

    Attached Files:

  6. DonManfred

    DonManfred Expert Licensed User

    it does.

    Please note that you are using the same key for all the values.
    Code:
    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
    Code:
    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.

    Code:
    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.

    Code:
    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: Jan 9, 2019
    Erel likes this.
  7. asubias

    asubias Member Licensed User

    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
     
  8. DonManfred

    DonManfred Expert Licensed User

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

    asubias Member Licensed User

    Ok, thank you again.

    Best regards,
    Alberto
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice