【问题标题】:Where is the keystore?密钥库在哪里?
【发布时间】:2017-03-09 22:31:07
【问题描述】:

我目前在我的 android 应用程序中存储了一个 RSA 密钥,它将用于用户应用程序数据的应用程序加密。

我生成密钥并将其存储为密钥目录中的文件,如下所示:

public RSA(char[] password) throws Exception
{
    private static final String filePath =   System.getProperty("user.dir") + "/keys/";
    mCurrentPassword = password;
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    File possibleKeyFile = new File(filePath + "/" + keyAlias);
    //FileInputStream keyFile = new FileInputStream(filePath + "/" + keyAlias);

    if(!possibleKeyFile.exists()) //if keystore is empty, create a key
    {
        mCurrentCertificate = generateCert();
        //Store the new keypair
        ks.load(null, password);

        KeyStore.ProtectionParameter protParam =
                new KeyStore.PasswordProtection(password);

        java.security.cert.Certificate[] myCert =
                new java.security.cert.Certificate[] {
                        (java.security.cert.Certificate) mCurrentCertificate
                };

        KeyStore.PrivateKeyEntry pkEntry =
                new KeyStore.PrivateKeyEntry(mCurrentRSAKeyPair.getPrivate(),
                        myCert);

        ks.setEntry(keyAlias, pkEntry, protParam);
        OutputStream os = new FileOutputStream(new File(filePath + "/" + keyAlias));

        ks.store(os , password);

    }
    else
    {
        //retrieve keypair and assign it to mCurrentKeyPair
        mCurrentRSAKeyPair = getKey(keyAlias, password);
    }
}

当我在调试模式下检索我的 android 上的密钥库时,我收到一条错误消息,指出文件不存在(目录无效)。我想知道如果存储路径设置为那样,密钥实际存储在哪里?

获取密钥对的代码:

private static KeyPair getKey(String alias, char[] password) throws Exception
{
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    FileInputStream keyFile = new FileInputStream(filePath + "/" + alias);
    ks.load(keyFile, password);

    KeyStore.ProtectionParameter protParam =
            new KeyStore.PasswordProtection(password);

    KeyStore.PrivateKeyEntry privateKeyEntry =
            (KeyStore.PrivateKeyEntry) ks.getEntry(alias, protParam);
    RSAPrivateKey privateKey = (RSAPrivateKey) privateKeyEntry.getPrivateKey();
    //get public key from private key
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(priv.getModulus(), BigInteger.valueOf(65537));
    PublicKey publicKey = kf.generatePublic(keySpec);

    KeyPair kp = new KeyPair(publicKey, privateKey);

    return kp;
}

【问题讨论】:

  • filePath 设置为什么?这就是您将密钥存储到

标签: java android rsa keystore


【解决方案1】:
OutputStream os = new FileOutputStream(new File(filePath + "/" + keyAlias));

在执行这一行之前,你应该执行这一行:

filePath.mkdirs();

目前可能目录“keys”不存在,而您没有采取任何措施来确保它存在。

您还需要在存储密钥库后关闭输出流。看来您也在某处忽略了IOExceptionFileNotFoundException

【讨论】:

  • 您好,路径已经创建,并且在检索密钥库而不是创建密钥库时失败。干杯
  • 所以alias 的值与您创建它时的值不同。当然,您根本不需要 alias 作为文件名的一部分,因为密钥库可以包含多个别名。您可以只使用固定的文件名,例如System.getProperty("user.dir") + "/.keystore"。当然,如果您已经拥有密钥对和证书,为什么还需要密钥库文件?
  • 嗨,我需要密钥库,这样我每次重新加载应用程序时都可以保留相同的密钥对和证书,而不是每次都擦除数据并重新生成它
  • 存储和检索目录相同,但找不到文件
猜你喜欢
  • 2019-03-10
  • 2011-04-05
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 2012-11-14
相关资源
最近更新 更多