簡體   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