下面我写的这段代码编译时没有错,可是在运行后却发现加密和解密后的文件大小均为0字节,很是奇怪,同样的方法我用在DES算法上面却是正确的。。
是什么原因导致这个结果的呢??高手帮忙解决下啊~~不胜感激~~      public class MyRSA {
/**
 * 把成生的一对密钥保存到RSAKey.xml文件中
 */
public void saveRSAKey() {
try {
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
kg.initialize(1024, sr);
FileOutputStream fos = new FileOutputStream("c:/test/RSAKey.xml");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
oos.writeObject(kg.generateKeyPair());
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("获取密钥对成功!");
} /**
 * 获得RSA加密的密钥。
 * 
 * @return KeyPair返回对称密钥
 */
public static KeyPair getKeyPair() {
// 产生新密钥对
KeyPair kp = null;
try {
String fileName = "c:/test/RSAKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (KeyPair) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
} /**
 * 文件file进行加密并保存目标文件destFile中
 * 
 * @param srcFileName
 *            要加密的文件 如c:/test/srcFile.txt
 * @param destFileName
 *            加密后存放的文件名 如c:/加密后文件.txt
 */
public static void encryptFile(String srcFileName, String destFileName)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, getKeyPair().getPublic());
InputStream is = new FileInputStream(srcFileName);
OutputStream out = new FileOutputStream(destFileName);
CipherInputStream cis = new CipherInputStream(is,cipher);
byte[] buffer = new byte[100];
int r;
while((r=cis.read(buffer))>=0){
out.write(buffer,0,r);
}
cis.close();
out.close();
is.close();
System.out.println("文件加密处理结束!");
} /**
 * 文件file进行加密并保存目标文件destFile中
 * 
 * @param srcFileName
 *            已加密的文件 如c:/加密后文件.txt
 * @param destFileName
 *            解密后存放的文件名 如c:/ test/解密后文件.txt
 */
public static void decryptFile(String srcFileName, String destFileName)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, getKeyPair().getPrivate());
InputStream is = new FileInputStream(srcFileName);
OutputStream out = new FileOutputStream(destFileName);
CipherOutputStream cos = new CipherOutputStream(out,cipher);
byte[] buffer = new byte[128];
int r;
while((r=is.read(buffer))>=0){
cos.write(buffer,0,r);
}
cos.close();
out.close();
is.close();
System.out.println("文件解密处理结束!");
}

public static void main(String[] args) {
MyRSA fileUtils=new MyRSA();
fileUtils.saveRSAKey();
MyRSA.getKeyPair();
try {
MyRSA.encryptFile("c:/test/XMLIndexAdvisorTechReport.pdf", "c:/test/加密后XMLIndexAdvisorTechReport.pdf");
} catch (Exception e) { e.printStackTrace();
}
try {
MyRSA.decryptFile("c:/test/加密后XMLIndexAdvisorTechReport.pdf", "c:/test/解密后XMLIndexAdvisorTechReport.pdf");
} catch (Exception e) {

e.printStackTrace();
}
}}

解决方案 »

  1.   


    public class MyRSA { 
    /** 
    * 把成生的一对密钥保存到RSAKey.xml文件中 
    */ 
    public void saveRSAKey() { 
    try { 
    SecureRandom sr = new SecureRandom(); 
    KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA"); 
    kg.initialize(1024, sr); 
    FileOutputStream fos = new FileOutputStream("c:/test/RSAKey.xml"); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    // 生成密钥 
    oos.writeObject(kg.generateKeyPair()); 
    oos.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 

    System.out.println("获取密钥对成功!"); 
    } /** 
    * 获得RSA加密的密钥。 

    * @return KeyPair返回对称密钥 
    */ 
    public static KeyPair getKeyPair() { 
    // 产生新密钥对 
    KeyPair kp = null; 
    try { 
    String fileName = "c:/test/RSAKey.xml"; 
    FileInputStream is = new FileInputStream(fileName); 
    ObjectInputStream oos = new ObjectInputStream(is); 
    kp = (KeyPair) oos.readObject(); 
    oos.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 

    return kp; 
    } /** 
    * 文件file进行加密并保存目标文件destFile中 

    * @param srcFileName 
    *            要加密的文件 如c:/test/srcFile.txt 
    * @param destFileName 
    *            加密后存放的文件名 如c:/加密后文件.txt 
    */ 
    public static void encryptFile(String srcFileName, String destFileName) 
    throws Exception { 
    Cipher cipher = Cipher.getInstance("RSA"); 
    cipher.init(Cipher.ENCRYPT_MODE, getKeyPair().getPublic()); 
    InputStream is = new FileInputStream(srcFileName); 
    OutputStream out = new FileOutputStream(destFileName); 
    CipherInputStream cis = new CipherInputStream(is,cipher); 
    byte[] buffer = new byte[100]; 
    int r; 
    while((r=cis.read(buffer))>=0){ 
    out.write(buffer,0,r); 

    cis.close(); 
    out.close(); 
    is.close(); 
    System.out.println("文件加密处理结束!"); 
    } /** 
    * 文件file进行加密并保存目标文件destFile中 

    * @param srcFileName 
    *            已加密的文件 如c:/加密后文件.txt 
    * @param destFileName 
    *            解密后存放的文件名 如c:/ test/解密后文件.txt 
    */ 
    public static void decryptFile(String srcFileName, String destFileName) 
    throws Exception { 
    Cipher cipher = Cipher.getInstance("RSA"); 
    cipher.init(Cipher.DECRYPT_MODE, getKeyPair().getPrivate()); 
    InputStream is = new FileInputStream(srcFileName); 
    OutputStream out = new FileOutputStream(destFileName); 
    CipherOutputStream cos = new CipherOutputStream(out,cipher); 
    byte[] buffer = new byte[128]; 
    int r; 
    while((r=is.read(buffer))>=0){ 
    cos.write(buffer,0,r); 

    cos.close(); 
    out.close(); 
    is.close(); 
    System.out.println("文件解密处理结束!"); 
    } public static void main(String[] args) { 
    MyRSA fileUtils=new MyRSA(); 
    fileUtils.saveRSAKey(); 
    MyRSA.getKeyPair(); 
    try { 
    MyRSA.encryptFile("c:/test/XMLIndexAdvisorTechReport.pdf", "c:/test/加密后XMLIndexAdvisorTechReport.pdf"); 
    } catch (Exception e) { e.printStackTrace(); 

    try { 
    MyRSA.decryptFile("c:/test/加密后XMLIndexAdvisorTechReport.pdf", "c:/test/解密后XMLIndexAdvisorTechReport.pdf"); 
    } catch (Exception e) { e.printStackTrace(); 

    } }粗略地看了一下沒問題,是不是文件名的問題
      

  2.   

    文件名问题??应该不会吧,换另一种对RSA的实现方法,加解密后的文件大小正常~
      

  3.   

    想不通為何你讀取文件時,一讀就返回-1int r; 
    while((r=cis.read(buffer))>=0){ 
    out.write(buffer,0,r); 

      

  4.   

    我知道了,RSA算法必须分块处理,不能一下子读进去后处理。。
      

  5.   

    那你怎麼解決?
    ECB/PKCS1Padding