6
我在使用RSA公钥解密解密文件时遇到问题。我的过程是接收XML文件,加密内容,并将其写回到同一个文件。另一个功能解密内容。我的源代码是:解密加密的文件内容?
public void decryptFile(String fileName,PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
FileInputStream fis = new FileInputStream(fileName);
File file=new File("decryptedfile.xml");
if(file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream("decryptedfile.xml");
CipherInputStream cis = new CipherInputStream(fis, cipher);
int i;
byte[] block = new byte[32];
//System.out.println("Read : "+cis.read(block));
while ((i = cis.read(block)) != -1) {
System.out.println(String.valueOf(i));
fos.write(block, 0, i);
}
fos.close();
}
我只是将加密文件的名称和相应的私钥值传入函数中。但是cis.read(block)
在第一次尝试时返回-1。任何人都可以建议我如何解密加密文件?
你确定整个文件是用RSA加密的吗?这是非常罕见的(太慢)。通常使用AES(例如AES)的混合加密用于文件(使用随机AES密钥加密文件,然后使用RSA加密AES密钥)。 – Robert
你确定你正在阅读的文件是否存在并且包含数据? –
无论您拥有多少数据,RSA密码实例只会对* one *块进行加密或解密。这就是为什么一个块通常包含对称加密密钥(如AES密钥)的原因。 –