Android Question key store save code sensitive data

ShadTech

Member
Licensed User
i have asked chatgpt for way to keep my app sensitive data not exposed
and it gives keystore method with this code
B4X:
Sub GenerateAndStoreKey
         Dim ks As JavaObject
        ks.InitializeStatic("java.security.KeyStore")
        ks = ks.RunMethod("getInstance", Array("AndroidKeyStore"))

         ks.RunMethod("load", Null) ' 🔥 التعديل هنا

         Dim keyGen As JavaObject
        keyGen.InitializeStatic("javax.crypto.KeyGenerator")
        keyGen = keyGen.RunMethod("getInstance", Array("AES", "AndroidKeyStore"))

         Dim purpose As Int = Bit.Or(1, 2) ' KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
        Dim builder As JavaObject
        builder.InitializeNewInstance("android.security.keystore.KeyGenParameterSpec$Builder", Array("MySecureKey", purpose))

         Dim modes As Object = Array As String("GCM")
        Dim paddings As Object = Array As String("NoPadding")

        builder.RunMethod("setBlockModes", Array(modes))
        builder.RunMethod("setEncryptionPaddings", Array(paddings))
        builder.RunMethod("setKeySize", Array(256))

         Dim spec As JavaObject = builder.RunMethod("build", Null)

         keyGen.RunMethod("init", Array(spec))
        keyGen.RunMethod("generateKey", Null)

 
End Sub






Sub GetSecretKey  As JavaObject
    Dim ks As JavaObject
    ks.InitializeStatic("java.security.KeyStore")
    ks = ks.RunMethod("getInstance", Array("AndroidKeyStore"))
    ks.RunMethod("load", Null)

    Dim key As JavaObject = ks.RunMethod("getKey", Array("MySecureKey", Null))
    If key.IsInitialized Then
        Return key
    Else
        Log("Key not found!")
        Return Null
    End If
End Sub

but it gives this error which chatgpt really cant fix( i have tried many times)

** Activity (main) Create (first time) **
main_vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv4 (java line: 848)
java.lang.RuntimeException: Method: load not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at com.TS.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv4(main.java:848)
at com.TS.main._activity_create(main.java:447)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at com.TS.main.afterFirstLayout(main.java:111)
at com.TS.main.access$000(main.java:23)
at com.TS.main$WaitForLayout.run(main.java:89)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
how b4a save data in keystore and retrieve from it??
Is that available in b4a and what is the code function to do that??
 

aeric

Expert
Licensed User
Longtime User
The code provided by ChatGPT is too complicated.
You just need to use [B4X] KVS - KeyValueStore library, add KVS_ENCRYPTION in build configuration (Ctrl + B) and follow the configuration in the spoiler.

 
Upvote 0

emexes

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim Yek As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    Yek = Bit.ShiftLeft(DateTime.Now.As(String).Length, 23)
    'other code here
    Dim Drowssap As String = Unshade("F58DDCC5F566F4447C836BC2373419E963131709A8C932C679F55F81C76B60277A3118C11F8D02E1AA48D0CA23234B7E4DFE77649362874A")
    Log(Drowssap)
End Sub

Sub Unshade(S As String) As String
    Dim bc As ByteConverter, cip As B4XCipher
    Return bc.StringFromBytes(cip.Decrypt(bc.HexToBytes(S), Yek * 17), "UTF-8")
End Sub
Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of main
--------- beginning of system
Shady's Secret Sentence

where, even without obfuscation, the key is still not readable after running the APK through decompiler.com:
Java:
public static String _process_globals() throws Exception {
    _xui = new B4XViewWrapper.XUI();
    _yek = 0;
    return "";
}

public static String _activity_create(boolean z) throws Exception {
    main main = mostCurrent;
    main._activity.LoadLayout("Layout", main.activityBA);
    Bit bit = Common.Bit;
    DateTime dateTime = Common.DateTime;
    _yek = Bit.ShiftLeft(BA.NumberToString(DateTime.getNow()).length(), 23);
    Common.LogImpl("1131077", _unshade("F58DDCC5F566F4447C836BC2373419E963131709A8C932C679F55F81C76B60277A3118C11F8D02E1AA48D0CA23234B7E4DFE77649362874A"), 0);
    return "";
}

public static String _unshade(String str) throws Exception {
    ByteConverter byteConverter = new ByteConverter();
    return byteConverter.StringFromBytes(new B4XEncryption().Decrypt(byteConverter.HexToBytes(str), BA.NumberToString(_yek * 17)), "UTF-8");
}
 
Last edited:
Upvote 0
Top