我能够在java中使用RSA算法加密和解密.txt文件。 如何为.rtf和.docx文件执行相同的操作?
更新:其他文件正在加密但生成零字节大小文件!!
这是代码
public void encrypt(String inputFile, String outputFile, PublicKey publicKey) throws InvalidKeyException, IOException {
cipher = Cipher.getInstance(RSA);
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
kpg.initialize(1024);
KeyPair keyPair = kpg.generateKeyPair();
privKey = keyPair.getPrivate();
pubKey = keyPair.getPublic();
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] block = new byte[1024];
int i;
try {
while ((i = fis.read(block)) != -1) {
cos.write(block, 0, i);
}
} catch (Exception e) {
throw new IOException(e);
} finally {
cos.close();
}
}
即使我在文本文件中复制简单的xml内容并对其进行加密,我也会获得零字节加密文件:(
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>