繁体   English   中英

无法从KeyStore检索私钥

[英]Unable to retrieve Private Key from KeyStore

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

这是我创建密钥的方式;

  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);

            }

这就是我尝试解密消息的方式。

定义密钥库;

 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)

我刚刚注意到我正在从另一个活动访问此方法,并且在尝试访问该方法时并未触发onCreate()方法。 我加了;

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

线指向cryptoString()方法。 之后,我的问题解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM