在Java中使用.pfx证书解密文件

在Java中使用.pfx证书解密文件,java,certificate,x509certificate,pfx,Java,Certificate,X509certificate,Pfx,我有一个.pfx文件和该文件的密码 我想用Java解密一个RSA加密的文件。 基本上与这里(c#)的方法相同,但在java中: 这可能吗 我目前的做法是: byte[] file = Files.readAllBytes(Paths.get("C:/file.fileinfo")); String pfxPassword = "pwd"; String keyAlias = "pvktmp:1ce254e5-4620-4abf-9a12-fb

我有一个.pfx文件和该文件的密码

我想用Java解密一个RSA加密的文件。 基本上与这里(c#)的方法相同,但在java中:

这可能吗

我目前的做法是:

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)

不幸的是,您没有提供keystore*pfx文件或加密文件,因此我必须设置自己的文件。如果您想使用我的文件运行我的示例,可以在此处获取它们:

您的源代码未在中显示inputStream值

keystore.load(inputStream, pfxPassword.toCharArray());
我使用了密钥库的直接加载:

keystore.load(new FileInputStream("keystore.pfx"), pfxPassword.toCharArray());
使用我的实现,我可以使用以下控制台输出成功地解密加密文件(“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密钥库对RSA加密文件进行解密。您在实现中看到的错误似乎导致用于加密(证书中的公钥)的密钥(对)在此期间发生了更改,并且您正在尝试使用(可能是新生成的)其他私钥(具有相同别名)进行解密。要检查这一点,您需要提供密钥库和加密文件

以下是我正在使用的实现(我刚刚编辑了inputStream行):


简短的回答是的。只需提供一套完整的数据(pfx文件、密码、加密文件和预期的明文)和一些您正在使用的Java代码。谢谢您的回答,更新了我的帖子谢谢,让它正常工作。问题是
Cipher.getInstance(key.getAlgorithm())
仅设置“RSA”。相反,我必须使用Cipher.getInstance(“RSA/ECB/OAEPWithSHA-1和mgf1padding”)
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)));
    }
}