Android - 在密钥库中存储私钥

时间:2014-06-13 08:28:40

标签: android cryptography android-keystore

如何在Keystore中存储我的私钥(密码,用户名,Sha1),以便我可以使它们安全?我试过下面的代码,但没有运气。

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();
}

这是我得到的日志错误。

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

1 个答案:

答案 0 :(得分:1)

您遇到的问题将在异常跟踪中详细说明:

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

简单地说,你不能这样做:

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

您不需要将Context传递给Activity,它已经有一个(致电getContext()或仅使用this)。虽然在Java中使用构造函数是常见的,但Android在ActivitiesServices等中绕过了这种需求,有利于活动生命周期

请参阅What is Context in AndroidAndroid Activity Developer Guide