3
我在java中毀了一個非常簡單的RSA加密。但輸出文件似乎是空的。 同樣的事情工作,如果我嘗試沒有CipherOutputStream。我可以看到每個在循環中寫入 循環都沒有影響。 任何線索... 謝謝。CipherOutputStream與rsa一起使用
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
public class ExampleRSA {
private static String VIDEO_SOURCE_FILE = "C:/Users/ggoldman/Desktop/Video/inputVideo.dv";
private static String EncryptedFile = "C:/Users/ggoldman/Desktop/Video/encVideo.dv";
private static File decfile = new File("C:/Users/ggoldman/Desktop/Video/decVideo.dv");
private static File incfile = new File(EncryptedFile);
private static File sourceMedia = new File(VIDEO_SOURCE_FILE);
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.generateKeyPair();
PrivateKey privKey = keyPair.getPrivate();
PublicKey pubKey = keyPair.getPublic();
// Encrypt
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
FileInputStream fis = new FileInputStream(sourceMedia);
FileOutputStream fos = new FileOutputStream(incfile);
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();
}
適用於我的txt files.Check你的路徑和文件權限 – Yahor10
@ Yahor10,行爲在我的機器上重現。它似乎取決於輸入莫名其妙。 –
發現這個問題:http://stackoverflow.com/questions/2221730/my-cipheroutputstream-fails-silently –