我试图在Android上实现SSL连接,但是当我尝试加载我的密钥库时我遇到了问题,但是当我调用KeyStore.getInstance(“RSA”)时,我得到了这个例外。
03-26 12:19:28.660:E / AndroidRuntime(6465):引起:java.lang.RuntimeException:java.security.KeyStoreException:java.security.NoSuchAlgorithmException:未找到KeyStore RSA实现
private KeyStore loadKeyStore() {
if (keyStore != null) {
Log.i("WSclient::KeyStore", " keyStore!=null");
return keyStore;
}
try {
Log.i("WSclient::KeyStore", " keyStore.getInstancel");
keyStore = KeyStore.getInstance("RSA");
Log.i("WSclient::KeyStore", " keyStore:: inputStream");
InputStream in = context.getResources().openRawResource(R.raw.file);
try {
Log.i("WSclient::KeyStore", " keyStore.load");
keyStore.load(in, KEYSTORE_PASSWORD.toCharArray());
} finally {
in.close();
}
return keyStore;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
答案 0 :(得分:1)
例外说明了一切。没有RSA密钥库这样的东西。有JKS密钥库,PCKS#11密钥库,WindowsMY密钥库,各种东西。什么是你的,只有你知道。
答案 1 :(得分:0)
您可以为RSA算法创建私钥/公钥对,如下所示:
Calendar calendarValidityStart = Calendar.getInstance();
Calendar calendarValidityEnd = Calendar.getInstance();
calendarValidityEnd.add(Calendar.YEAR, 99);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias("MyKeyAlias")
.setSubject(new X500Principal("CN=" + "MyKeyAlias"))
.setSerialNumber(BigInteger.valueOf(1337))
.setStartDate(calendarValidityStart.getTime())
.setEndDate(calendarValidityEnd.getTime())
.build();
KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(
"RSA",
"AndroidKeyStore");
kpGenerator.initialize(spec);
KeyPair keyPair = kpGenerator.generateKeyPair();
您可以稍后从KeyStore中检索密钥对,如下所示:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.Entry keyStoreEntry = keyStore.getEntry(alias, null);
有关详情,请参阅Android开发者BasicAndroidKeyStore示例。
答案 2 :(得分:-1)
一定要
private KeyFactory keyStore;
keyStore
必须是KeyFactory
类型。
//keyStore = KeyStore.getInstance("RSA");
keyStore = KeyFactory.getInstance("RSA");
查看此示例: 的 How to use keystore in Java to store private key? 强>