我目前在我的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設置爲?這就是你存儲密鑰到 – dweebo