我相信我这样做是对的。我如何看待使用 RSA 解密文件:
- 读入文件的每一行作为
String
- 放
cipher.init(Cipher.DECRYPT_MODE, privateKey)
- 将字符串转换为
char[]
使用Hex.decodeHex(String.toCharArray())
- 最后做
cipher.doFinal(x)
这听起来对吗?我正在这样做,但它不起作用,DecryptedFile.txt
只是 2 行“null”。
我可以使用几乎完全相同的过程进行加密,但cipher.init(Cipher.ENCRYPT_MODE, publicKey)
很明显。
这是我的代码
try {
BufferedReader inStream = new BufferedReader (new FileReader(cryptoFile));
int k = 0;
fileContents.add(inStream.readLine());
while(fileContents.get(k) != null) {
k++;
fileContents.add(inStream.readLine());
}
inStream.close();
try {
PrivateKey privateKey = kp.getPrivate();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
int j = 0;
while(fileContents.get(j) != null) {
String text = fileContents.get(j);
try {
x = Hex.decodeHex(text.toCharArray());
y = cipher.doFinal(x);
} catch (DecoderException ex) {
Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex);
}
try (PrintWriter file = new PrintWriter(
new BufferedWriter(
new FileWriter("DecryptedFile.txt", true)))) {
file.println(y);
} catch (IOException e) {
System.err.println("IOERROR: " + e.getMessage() + "\n");
}
j++;
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException e) {
System.err.println("IOERROR: File NOT Found: " + cryptoFile + "\n");
} catch ( IOException e ) {
System.err.println("IOERROR: " + e.getMessage() + "\n");
} finally {
messagePane.setText(messagePane.getText() + "\n\n"
+ cryptoFile + "is done being decrypted.");
messagePane.setText(messagePane.getText() + "\n"
+ "Decrypted file saved to \'DecryptedFile.txt\'.");
cryptoFile = "";
pathTextField.setText(cryptoFile);
encryptButton.setEnabled(false);
decryptButton.setEnabled(false);
}