我试图使用this gist加密我的应用程序中的一些数据。KeyStore没有找到我的别名密钥
我已经用我的别名“Pablo”签名了我的apk。 问题是试图运行此代码:
public static String encrypt(String alias, String plaintext) {
try {
PublicKey publicKey = getPrivateKeyEntry(alias).getCertificate().getPublicKey();
Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return Base64.encodeToString(cipher.doFinal(plaintext.getBytes()), Base64.NO_WRAP);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static KeyStore.PrivateKeyEntry getPrivateKeyEntry(String alias) {
try {
KeyStore ks = KeyStore
.getInstance("AndroidKeyStore");
ks.load(null);
KeyStore.Entry entry = ks.getEntry(alias, null);
if (entry == null) {
Log.w(TAG, "No key found under alias: " + alias);
Log.w(TAG, "Exiting signData()...");
return null;
}
if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
Log.w(TAG, "Not an instance of a PrivateKeyEntry");
Log.w(TAG, "Exiting signData()...");
return null;
}
return (KeyStore.PrivateKeyEntry) entry;
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
return null;
}
}
但即时得到异常:“没有项下别名发现”
任何想法?我必须输入与我的jks相同的别名吗?
谢谢!
你尝试过在命令行列出密钥库别名HTTP://stackoverflow.com/a/12894334/1056359 – thepoosh
是,别名它的确定,我试着它调试与已签名的apk –
哪行代码发生异常?确保您正在使用正在运行应用程序的jre中的正确密钥库。使用的运行时JRE可以是不同的,包含不带别名的不同密钥库。 – Sonic