gpt4 book ai didi

Android 4.3 KeyStore - 尝试检索 key 时链 == null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:51 27 4
gpt4 key购买 nike

正在关注 this blog ,我正在使用此代码在 Android KeyStore 中创建和存储 KeyPair:

Context ctx = getApplicationContext();
Calendar notBefore = Calendar.getInstance();
Calendar notAfter = Calendar.getInstance();
notAfter.add(1, Calendar.YEAR);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx).
setAlias(RSA_KEYS_ALIAS).setSubject(
new X500Principal(String.format("CN=%s, OU=%s",
getApplicationName(), ctx.getPackageName()))).
setSerialNumber(BigInteger.ONE).
setStartDate(notBefore.getTime()).setEndDate(notAfter.getTime()).build();

KeyPairGenerator kpGenerator;
try {
kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");

kpGenerator.initialize(spec);
kpGenerator.generateKeyPair();
} catch (Exception e) {
showException(e);
}

当我尝试使用此代码从 KeyStore 检索公钥时,会抛出一个带有消息 chain == nullNullPointerException

public RSAPublicKey getRSAPublicKey() {
RSAPublicKey result = null;
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.PrivateKeyEntry keyEntry =
(KeyStore.PrivateKeyEntry) keyStore.getEntry(RSA_KEYS_ALIAS, null); // --< exception is thrown here
result = (RSAPublicKey) keyEntry.getCertificate().getPublicKey();
}
} catch (Exception e) {
showException(e);
}
return result;
}

获取私钥的代码也是如此。

更新:

我将我的代码与 Google BasicAndroidKeyStore sample 进行了比较.该示例中生成、存储和检索 key 对的机制实际上与我实现的相同。我很困惑为什么这段代码在完美运行了几个月后就停止运行了。

如有任何建议或提示,我们将不胜感激。

最佳答案

显然,Android KeyStore 中的名称在所有应用程序中必须是唯一的。我有另一个应用程序,它的键使用相同的名称。更改两个应用程序使用的公共(public)库来创建和使用 key 以在其 key 名称中包含包名称后,问题就消失了......

关于Android 4.3 KeyStore - 尝试检索 key 时链 == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977407/

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