0

How can I store my private keys(password, username, Sha1) in Keystore so that I can make them secure? I have tried below code but no luck.

private Context ctx;

public MainActivity(Context ctx) {
    this.ctx = ctx;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        genKey();
    } catch (Exception e) {

        e.printStackTrace();
        Log.d("warning", "m in catch");
    }

}

public void genKey() throws Exception {
    SecretKey key = KeyGenerator.getInstance("AES").generateKey();

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, "clavedekey".toCharArray());

    PasswordProtection pass = new PasswordProtection(
            "fedsgjk".toCharArray());
    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key);
    ks.setEntry("secretKeyAlias", skEntry, pass);

    FileOutputStream fos = ctx.openFileOutput("bs.keystore",
            Context.MODE_PRIVATE);
    ks.store(fos, "clavedekey".toCharArray());
    fos.close();
}

here are the log error I am getting.

06-13 14:04:11.829: E/AndroidRuntime(6248): FATAL EXCEPTION: main
06-13 14:04:11.829: E/AndroidRuntime(6248): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.demo_keystore/com.demo_keystore.MainActivity}: java.lang.InstantiationException: can't instantiate class com.demo_keystore.MainActivity; no empty constructor
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.os.Looper.loop(Looper.java:177)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread.main(ActivityThread.java:4947)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at java.lang.reflect.Method.invokeNative(Native Method)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at java.lang.reflect.Method.invoke(Method.java:511)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at dalvik.system.NativeStart.main(Native Method)
06-13 14:04:11.829: E/AndroidRuntime(6248): Caused by: java.lang.InstantiationException: can't instantiate class com.demo_keystore.MainActivity; no empty constructor
06-13 14:04:11.829: E/AndroidRuntime(6248):     at java.lang.Class.newInstanceImpl(Native Method)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at java.lang.Class.newInstance(Class.java:1319)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
06-13 14:04:11.829: E/AndroidRuntime(6248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
06-13 14:04:11.829: E/AndroidRuntime(6248):     ... 11 more
Bach
  • 6,145
  • 7
  • 36
  • 61
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30

1 Answers1

1

The issue you are facing is detailed in the exception trace:

Caused by: java.lang.InstantiationException: can't instantiate class 
           com.demo_keystore.MainActivity; no empty constructor

Simply put, you cant do this:

public MainActivity(Context ctx) {
    this.ctx = ctx;
}

You don't need to pass a Context through to an Activity, it already has one (call getContext() or just use this). Whereas its common to have constructors in Java, Android bypasses this need in Activities, Services etc, favouring the Activity lifecycle.

See What is Context in Android and Android Activity Developer Guide.

Community
  • 1
  • 1
biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • yes i got resolved my crash but i wanna store my keys any help??/ – Shani Goriwal Jun 13 '14 at 08:56
  • Some resources I found: [How to use keystore in java](http://stackoverflow.com/a/9890863/637545) and [Java Cryptography by Oracle](http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html) – biddulph.r Jun 13 '14 at 08:59