gpt4 book ai didi

java - Android KeyStore 不保存 RSA key

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:29 24 4
gpt4 key购买 nike

我正在使用 android keystore api 来存储 RSA key 对。

当我在 Debug模式下运行 Android 时,它显示 key 对和证书字节已存储在 keystore 中,但是当我重新加载调试器并尝试检索 key 时, keystore 似乎为空。

我希望能够通过别名检索 key 。我下面的代码显示了如何创建和存储 key 对。

    public RSA(char[] password) throws Exception {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
Enumeration<String> aliases = ks.aliases();
if(!aliases.hasMoreElements())
{
mCurrentCertificate = generateCert();
//Store the new keypair
FileInputStream fs = null;
ks.load(fs, 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("MyKey2", pkEntry, protParam);

for(int i = 0; i < ks.size(); i++)
{
//MyKey is the previous key stored, MyKey2 is the next one
System.out.println(ks.getCertificate("MyKey"));
}
}
}

最佳答案

使用您的代码,您可以在文件系统中创建 keystore (缺少 ks.store(fileOutputStream, password); ,但 Android Keystore System 有一个特定的 API 来生成和使用 key

在此answer您有一个生成和加载 RSA key 的示例

关于java - Android KeyStore 不保存 RSA key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com