既にファイル (plaintext.txt) を暗号化しています。次に、このファイルを再度暗号化する必要があります。ただし、暗号化されたファイルが読み取れないためかどうかはわかりません。そのため、再度暗号化できるように、バイナリ ファイルまたはその他の読み取り可能なファイルに変換したいと考えています。
まず、このコード (公開鍵と秘密鍵が生成されています) を使用して、encrypt.txt という名前の .txt ファイルを暗号化しました。
暗号化されたファイルを暗号化できるかどうかを確認するために、同じコードを使用して「encrypt.txt」ファイルを再暗号化し、再暗号化されたファイル「encrypt2.txt」を生成します。
ただし、「encrypt2.txt」ファイルには情報がなく、このファイル サイズも 0kb です。
したがって、私が求めているのは、これを使用して再暗号化できるかどうかです。
はいの場合、「encrypt2.txt」に情報が存在しないのはどうしてですか。それ以外の場合、この再暗号化を行うにはどうすればよいですか?
いつもイルミネーションをありがとう!
以下は私のコードです。
public static void main(String[] args) throws Exception {
Security.addProvider(new FlexiCoreProvider());
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "FlexiCore");
Cipher cipher = Cipher.getInstance("RSA", "FlexiCore");
kpg.initialize(1024);
KeyPair keyPair = kpg.generateKeyPair();
PrivateKey privKey = keyPair.getPrivate();
PublicKey pubKey = keyPair.getPublic();
// Encrypt
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String cleartextFile = "cleartext.txt";
String ciphertextFile = "ciphertextRSA.txt";
FileInputStream fis = new FileInputStream(cleartextFile);
FileOutputStream fos = new FileOutputStream(ciphertextFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] block = new byte[32];
int i;
while ((i = fis.read(block)) != -1) {
cos.write(block, 0, i);
}
cos.close();
// Decrypt
String cleartextAgainFile = "cleartextAgainRSA.txt";
cipher.init(Cipher.DECRYPT_MODE, privKey);
fis = new FileInputStream(ciphertextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(cleartextAgainFile);
while ((i = cis.read(block)) != -1) {
fos.write(block, 0, i);
}
fos.close();
}