用RSA加密文件成功,解密不成功,请各位大虾帮忙解决一下……
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
import java.util.Scanner;
import java.io.*;
public class RSAEncrypt {
Scanner input = new Scanner(System.in);
String path;
FileInputStream fileInputStream;
BufferedWriter bufferedWriter;
FileWriter fileWriter;
byte[] buf = new byte[64];
byte[] de;
byte[] e;
int bytenum;
String str;
protected void encryptFile(RSAPublicKey publicKey)
{
try {
System.out.println("Input the path of the file you want to encrypt:");
path = input.next();
fileInputStream = new FileInputStream(path);
System.out.println("The file has been encrypt!please input the path:");
path = input.next();
fileWriter = new FileWriter(path);
bufferedWriter = new BufferedWriter(fileWriter);
bytenum = fileInputStream.read(buf);
while(bytenum!=-1)
{
e = encrypt(publicKey, buf);
str = bytesToString(e);
bufferedWriter.write(str);
bytenum = fileInputStream.read(buf);
}
fileInputStream.close();
bufferedWriter.close();
}catch(IOException e) {
e.printStackTrace();
System.err.println(e);
}
}
protected void decryptFile(RSAPrivateKey privateKey)
{
try {
System.out.println("Input the path of the file you want to decrypt:");
path = input.next();
fileInputStream = new FileInputStream(path);
System.out.println("The file has been decrypt!please input the path:");
path = input.next();
fileWriter = new FileWriter(path);
bufferedWriter = new BufferedWriter(fileWriter);
bytenum = fileInputStream.read(buf);
while (bytenum != -1) {
de = decrypt(privateKey, buf);
str = bytesToString(de);
bufferedWriter.write(str);
bytenum = fileInputStream.read(buf);
}
bufferedWriter.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.err.println(e);
}
}
protected String bytesToString(byte[] encrytpByte) {
String result = "";
for (Byte bytes : encrytpByte) {
result += (char) bytes.intValue();
}
return result;
}
protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
RSAEncrypt encrypt = new RSAEncrypt();
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
encrypt.encryptFile(publicKey);
encrypt.decryptFile(privateKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}
------解决方案--------------------------------------------------------