如何在Cordova插件中保存PrivateKey证书

时间:2017-10-17 17:07:50

标签: java android cordova cordova-plugins

我需要保存并使用来自cordova插件的PrivateKey到另一个cordova插件。我的问题是,如何保存此密钥并在另一个cordova插件中使用它或在回调中发送此密钥或将此密钥保存到PEM文件。

这是Cordova插件的Java代码

public class CertificatePlugin extends CordovaPlugin implements KeyChainAliasCallback {
  private static String[] KEYTYPES = {"PKCS12"};
  CallbackContext callbackContext = null;
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext
                      callbackContext) throws JSONException {
    this.callbackContext = callbackContext;
    if ("selectCert".equals(action)) {
        return selectCertificate();

    }
    return false;
  }

  private boolean selectCertificate() {
    Activity activity = this.cordova.getActivity();
    KeyChain.choosePrivateKeyAlias(activity, this, KEYTYPES, null,
                                   null, -1, null);

    return true;
  }


  private PrivateKey getPrivateKey(String alias) {

    try {
        PrivateKey pk = KeyChain.getPrivateKey(cordova.getActivity().getApplicationContext(), alias);
        return pk;
    } catch (KeyChainException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
 }

  @Override
  public void alias(String alias) {
    callbackContext.success(alias);
    PrivateKey key = getPrivateKey (alias);   
 }  

}

0 个答案:

没有答案