簡體   English   中英

使用Java中的.pfx證書解密文件

[英]Decrypt file using .pfx certificate in Java

我有一個 .pfx 文件和該文件的密碼。

我想使用 Java 解密 RSA 加密文件。 基本上與此處相同的方法(c#),但在 java: https://stackoverflow.com/a/37894914/13329087

這可能嗎?

到目前為止我的方法:

byte[] file = Files.readAllBytes(Paths.get("C:/file.fileinfo"));

String pfxPassword = "pwd";
String keyAlias = "pvktmp:1ce254e5-4620-4abf-9a12-fbbda5b97fa0";

KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream("/keystore.pfx"), pfxPassword.toCharArray());
PrivateKey key = (PrivateKey)keystore.getKey(keyAlias, pfxPassword.toCharArray());

Cipher cipher = Cipher.getInstance(key.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, key);
System.out.println(new String(cipher.doFinal(file)));

這會產生一個錯誤:

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:379)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:290)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
    at javax.crypto.Cipher.doFinal(Cipher.java:2168)

不幸的是,您沒有提供密鑰庫 *pfx-file 和加密文件,所以我不得不設置自己的文件。 如果您想使用我的文件運行我的示例,您可以在此處獲取它們:

https://github.com/java-crypto/Stackoverflow/tree/master/Decrypt_using_pfx_certificate_in_Java

您的源代碼未顯示 inputStream-value

keystore.load(inputStream, pfxPassword.toCharArray());

我使用了直接加載密鑰庫:

keystore.load(new FileInputStream("keystore.pfx"), pfxPassword.toCharArray());

使用我的實現,我可以使用此控制台 output成功解密加密文件(“fileinfo”):

https://stackoverflow.com/questions/62769422/decrypt-using-pfx-certificate-in-java?noredirect=1#comment111047725_62769422
RSA decryption using a pfx keystore
The quick brown fox jumps over the lazy dog

回答您的問題:是的,可以使用 pfx-keystore 解密 RSA 加密文件 您在實現中看到的錯誤似乎導致用於加密的密鑰(對)(證書中的公鑰)在此期間發生了變化,並且您正在嘗試使用(可能是新生成的)其他私鑰解密(具有相同的別名)。 要檢查這一點,您需要提供密鑰庫和加密文件...

這是我正在使用的實現(我剛剛編輯了 inputStream-line):

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.CertificateException;

public class MainSo {
    public static void main(String[] args) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, CertificateException {
        System.out.println("https://stackoverflow.com/questions/62769422/decrypt-using-pfx-certificate-in-java?noredirect=1#comment111047725_62769422");
        System.out.println("RSA decryption using a pfx keystore");
        //byte[] file = Files.readAllBytes(Paths.get("C:/file.fileinfo"));
        byte[] file = Files.readAllBytes(Paths.get("fileinfo"));
        // password for keystore access and private key + certificate access
        String pfxPassword = "pwd";
        String keyAlias = "pvktmp:1ce254e5-4620-4abf-9a12-fbbda5b97fa0";
        // load keystore
        KeyStore keystore = KeyStore.getInstance("PKCS12");
        //keystore.load(inputStream, pfxPassword.toCharArray());
        keystore.load(new FileInputStream("keystore.pfx"), pfxPassword.toCharArray());
        PrivateKey key = (PrivateKey) keystore.getKey(keyAlias, pfxPassword.toCharArray());
        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);
        System.out.println(new String(cipher.doFinal(file)));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM