gpt4 book ai didi

android - 无法从 KeyStore 检索私钥

转载 作者:行者123 更新时间:2023-11-29 02:40:56 25 4
gpt4 key购买 nike

我正在尝试制作一个使用 RSA 算法进行加密的聊天应用程序。之后但是当我尝试解密消息时,我在下面收到此错误。

这是我创建 key 的方式;

  if (!keyStore.containsAlias(alias)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 25);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getApplicationContext())
.setAlias(alias)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
generator.initialize(spec);

}

下面是我尝试解密消息的方式;

定义KeyStore;

 static KeyStore keyStore;

在 onCreate 方法中加载 keyStore:

  try{
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
}
catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
e.printStackTrace();
}

解密函数:

      public String decryptString(String alias, String decrypted) {
try {

KeyStore.Entry entry;
//ERROR HAPPENS HERE.
entry = keyStore.getEntry(alias, null);

KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) entry;

Cipher output = Cipher.getInstance("RSA/ECB/PKCS1Padding");
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());


CipherInputStream cipherInputStream = new CipherInputStream(
new ByteArrayInputStream(Base64.decode(decrypted, Base64.DEFAULT)), output);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte) nextByte);
}

byte[] bytes = new byte[values.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i);
}

String finalText = new String(bytes, 0, bytes.length, "UTF-8");
return finalText;

} catch (IOException | KeyStoreException | NoSuchPaddingException | UnrecoverableEntryException | InvalidKeyException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "Error";
}

我在下面收到这个错误;

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.furkan.profil, PID: 10591
java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.KeyStore$Entry java.security.KeyStore.getEntry(java.lang.String, java.security.KeyStore$ProtectionParameter)' on a null object reference
at com.furkan.profil.RSAHelper.decryptString(RSAHelper.java:180)
at com.furkan.profil.Chat.ChatActivity$4.onChildAdded(ChatActivity.java:251)
at com.google.android.gms.internal.zzblz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

我刚刚注意到我正在从另一个 Activity 访问此方法,并且当我尝试访问此方法时我没有触发 onCreate() 方法。我补充说;

try{
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
} catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
throw new IllegalStateException("KeyStore could not be initialized", e);
}

decryptString() 方法的行。之后我的问题就解决了。

关于android - 无法从 KeyStore 检索私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44205888/

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