写的RSA算法,加密解密txt文件时没有没有任何问题,可对Word文档进行加密解密时,程序显示运行正常,可打开解密后的Word文档却打不开,出现“Word在试图打开文件时出现错误”,
百思不得其解,望各位帮忙
代码如下 一些判断,流的关闭就省了,呵呵:  public class RSAUtil
{
  private PrivateKey priKey;
  private PublicKey pubKey;
  public void initializeKeyPair()throws Exception
  {  KeyPairGenerator  keyGen=KeyPairGenerator.getInstance"RSA",eworg.bouncycastle.jce.provider.BouncyCastleProvider());
   keyGen.initialize(1024, new SecureRandom());
   KeyPair keypair = keyGen.generateKeyPair();
   priKey = keypair.getPrivate();
   pubKey = keypair.getPublic();
  }
//从流中读取出相应的字节数据
  private byte[] getBytesFromStream(InputStream is)throws Exception
  {
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
     int len = -1;
    while ((len = input.read(b)) != -1)
bos.write(b, 0, len);
    return bos.toByteArray();
  }
 //input 是明文文件, ciphertext是加密后的密文文件
  public int encrypt(File input, File ciphertext) throws Exception
  {
     FileInputStream fis = new FileInputStream(input)
     byte[] buffer = this.getBytesFromStream(fis);
    Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
     cipher.init(Cipher.ENCRYPT_MODE, pubKey);
     int dataLength = buffer.length;
     int blockSize = cipher.getBlockSize();
      ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
      int i=0;
    byte[] b = null;
    while(dataLength-i*blockSize>0)
     {
if(dataLength-i*blockSize>blockSize)
{
  b = cipher.doFinal(input, i*blockSize, blockSize);
  bos.write(b);
}
else
{
  b = cipher.doFinal(input,i*blockSize,dataLength-i*blockSize);
  bos.write(b);
}
i++;
     }
    FileOutputStream fos = new FileOututStream(ciphertext);
    fos.write(bos2.toByteArray());
    fos.close();
    return buffer  //加密的字节长度
   }
//input 是加密后的文件,plaintext是解密后的文件
public int decrypt(File input, File plaintext) throws Exception
{
  FileInputStream fis = new FileInputStream(input);
  byte[] buffer = this.getBytesFromStream(fis);
  Cipher cipher = Cipher.getInstance(ALGORITHM, new org.bouncycastle.jce.provider.BouncyCastleProvider());
  cipher.init(Cipher.DECRYPT_MODE, priKey);
  int blockSize = cipher.getBlockSize();
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  int dataLength = input.length;
  int i = 0;
  while (dataLength - i * blockSize > 0)
  {
    byte[] temp = cipher.doFinal(input, i * blockSize, blockSize);
    bos.write(temp);
    i++;
  }
   FileOutputStream fos = new FileOututStream(plaintext);
    fos.write(bos.toByteArray());
    fos.close();
    return buffer  //解密的字节长度
}
  public static void main(String args[])
  {
   RSAUtil rsa = new RSAUtil();
   rsa.initializeKeyPair();
   File plaintext = new File("F:\\1.doc");
   File ciphertext = new File("F:\\2.doc")
   rsa.encrypt(plaintext,ciphertext);
   
   File plainAnother = new File("F:\\3.doc");
   rsa.decrypt(ciphertext,plainAnother);
  }
}运行时正常,可打不开3.doc这个文件,而且2.doc文件有很多页?
请高手指点一下,看问题出在哪了?

解决方案 »

  1.   

    谢了,当时没有试,只是在网页面上直接写的,少了些字母,呵呵是有问题, 下面的是修改完的,package com.gmc.test;import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;import javax.crypto.Cipher;public class Test
    {
      private PrivateKey priKey;
      private PublicKey pubKey;
      public void initializeKeyPair()throws Exception
      {  KeyPairGenerator  keyGen=KeyPairGenerator.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
       keyGen.initialize(1024, new SecureRandom());
       KeyPair keypair = keyGen.generateKeyPair();
       priKey = keypair.getPrivate();
       pubKey = keypair.getPublic();
      }
    //从流中读取出相应的字节数据
      private byte[] getBytesFromStream(InputStream is)throws Exception
      {
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
         int len = -1;
        while ((len = is.read(b)) != -1)
        bos.write(b, 0, len);
        return bos.toByteArray();
      }
     //input 是明文文件, ciphertext是加密后的密文文件
      public int encrypt(File input, File ciphertext) throws Exception
      {
         FileInputStream fis = new FileInputStream(input);
         byte[] buffer = this.getBytesFromStream(fis);
        Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
         cipher.init(Cipher.ENCRYPT_MODE, pubKey);
         int dataLength = buffer.length;
         int blockSize = cipher.getBlockSize();
          ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
          int i=0;
        byte[] b = null;
        while(dataLength-i*blockSize>0)
         {
        if(dataLength-i*blockSize>blockSize)
        {
          b = cipher.doFinal(buffer, i*blockSize, blockSize);
          bos2.write(b);
        }
        else
        {
          b = cipher.doFinal(buffer,i*blockSize,dataLength-i*blockSize);
          bos2.write(b);
        }
        i++;
         }
        FileOutputStream fos = new FileOutputStream(ciphertext);
        fos.write(bos2.toByteArray());
        fos.close();
        return buffer.length;  //加密的字节长度
       }
    //input 是加密后的文件,plaintext是解密后的文件
    public int decrypt(File input, File plaintext) throws Exception
    {
      FileInputStream fis = new FileInputStream(input);
      byte[] buffer = this.getBytesFromStream(fis);
      Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
      cipher.init(Cipher.DECRYPT_MODE, priKey);
      int blockSize = cipher.getBlockSize();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      int dataLength = buffer.length;
      int i = 0;
      while (dataLength - i * blockSize > 0)
      {
        byte[] temp = cipher.doFinal(buffer, i * blockSize, blockSize);
        bos.write(temp);
        i++;
      }
       FileOutputStream fos = new FileOutputStream(plaintext);
        fos.write(bos.toByteArray());
        fos.close();
        return dataLength; //解密的字节长度
    }
      public static void main(String args[]) throws Exception
      {
       Test rsa = new Test();
       rsa.initializeKeyPair();
       File plaintext = new File("F:\\1.doc");
       File ciphertext = new File("F:\\2.doc");
       rsa.encrypt(plaintext,ciphertext);
       
       File plainAnother = new File("F:\\3.doc");
       rsa.decrypt(ciphertext,plainAnother);
      }
    }
      

  2.   

    谢了,当时没有试,只是在网页面上直接写的,少了些字母,呵呵是有问题, 下面的是修改完的,package com.gmc.test;import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;import javax.crypto.Cipher;public class Test
    {
      private PrivateKey priKey;
      private PublicKey pubKey;
      public void initializeKeyPair()throws Exception
      {  KeyPairGenerator  keyGen=KeyPairGenerator.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
       keyGen.initialize(1024, new SecureRandom());
       KeyPair keypair = keyGen.generateKeyPair();
       priKey = keypair.getPrivate();
       pubKey = keypair.getPublic();
      }
    //从流中读取出相应的字节数据
      private byte[] getBytesFromStream(InputStream is)throws Exception
      {
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
         int len = -1;
        while ((len = is.read(b)) != -1)
        bos.write(b, 0, len);
        return bos.toByteArray();
      }
     //input 是明文文件, ciphertext是加密后的密文文件
      public int encrypt(File input, File ciphertext) throws Exception
      {
         FileInputStream fis = new FileInputStream(input);
         byte[] buffer = this.getBytesFromStream(fis);
        Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
         cipher.init(Cipher.ENCRYPT_MODE, pubKey);
         int dataLength = buffer.length;
         int blockSize = cipher.getBlockSize();
          ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
          int i=0;
        byte[] b = null;
        while(dataLength-i*blockSize>0)
         {
        if(dataLength-i*blockSize>blockSize)
        {
          b = cipher.doFinal(buffer, i*blockSize, blockSize);
          bos2.write(b);
        }
        else
        {
          b = cipher.doFinal(buffer,i*blockSize,dataLength-i*blockSize);
          bos2.write(b);
        }
        i++;
         }
        FileOutputStream fos = new FileOutputStream(ciphertext);
        fos.write(bos2.toByteArray());
        fos.close();
        return buffer.length;  //加密的字节长度
       }
    //input 是加密后的文件,plaintext是解密后的文件
    public int decrypt(File input, File plaintext) throws Exception
    {
      FileInputStream fis = new FileInputStream(input);
      byte[] buffer = this.getBytesFromStream(fis);
      Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
      cipher.init(Cipher.DECRYPT_MODE, priKey);
      int blockSize = cipher.getBlockSize();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      int dataLength = buffer.length;
      int i = 0;
      while (dataLength - i * blockSize > 0)
      {
        byte[] temp = cipher.doFinal(buffer, i * blockSize, blockSize);
        bos.write(temp);
        i++;
      }
       FileOutputStream fos = new FileOutputStream(plaintext);
        fos.write(bos.toByteArray());
        fos.close();
        return dataLength; //解密的字节长度
    }
      public static void main(String args[]) throws Exception
      {
       Test rsa = new Test();
       rsa.initializeKeyPair();
       File plaintext = new File("F:\\1.doc");
       File ciphertext = new File("F:\\2.doc");
       rsa.encrypt(plaintext,ciphertext);
       
       File plainAnother = new File("F:\\3.doc");
       rsa.decrypt(ciphertext,plainAnother);
      }
    }
      

  3.   

    谢了,当时没有试,只是在网页面上直接写的,少了些字母,呵呵是有问题, 下面的是修改完的,package com.gmc.test;import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;import javax.crypto.Cipher;public class Test
    {
      private PrivateKey priKey;
      private PublicKey pubKey;
      public void initializeKeyPair()throws Exception
      {  KeyPairGenerator  keyGen=KeyPairGenerator.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
       keyGen.initialize(1024, new SecureRandom());
       KeyPair keypair = keyGen.generateKeyPair();
       priKey = keypair.getPrivate();
       pubKey = keypair.getPublic();
      }
    //从流中读取出相应的字节数据
      private byte[] getBytesFromStream(InputStream is)throws Exception
      {
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
         int len = -1;
        while ((len = is.read(b)) != -1)
        bos.write(b, 0, len);
        return bos.toByteArray();
      }
     //input 是明文文件, ciphertext是加密后的密文文件
      public int encrypt(File input, File ciphertext) throws Exception
      {
         FileInputStream fis = new FileInputStream(input);
         byte[] buffer = this.getBytesFromStream(fis);
        Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
         cipher.init(Cipher.ENCRYPT_MODE, pubKey);
         int dataLength = buffer.length;
         int blockSize = cipher.getBlockSize();
          ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
          int i=0;
        byte[] b = null;
        while(dataLength-i*blockSize>0)
         {
        if(dataLength-i*blockSize>blockSize)
        {
          b = cipher.doFinal(buffer, i*blockSize, blockSize);
          bos2.write(b);
        }
        else
        {
          b = cipher.doFinal(buffer,i*blockSize,dataLength-i*blockSize);
          bos2.write(b);
        }
        i++;
         }
        FileOutputStream fos = new FileOutputStream(ciphertext);
        fos.write(bos2.toByteArray());
        fos.close();
        return buffer.length;  //加密的字节长度
       }
    //input 是加密后的文件,plaintext是解密后的文件
    public int decrypt(File input, File plaintext) throws Exception
    {
      FileInputStream fis = new FileInputStream(input);
      byte[] buffer = this.getBytesFromStream(fis);
      Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
      cipher.init(Cipher.DECRYPT_MODE, priKey);
      int blockSize = cipher.getBlockSize();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      int dataLength = buffer.length;
      int i = 0;
      while (dataLength - i * blockSize > 0)
      {
        byte[] temp = cipher.doFinal(buffer, i * blockSize, blockSize);
        bos.write(temp);
        i++;
      }
       FileOutputStream fos = new FileOutputStream(plaintext);
        fos.write(bos.toByteArray());
        fos.close();
        return dataLength; //解密的字节长度
    }
      public static void main(String args[]) throws Exception
      {
       Test rsa = new Test();
       rsa.initializeKeyPair();
       File plaintext = new File("F:\\1.doc");
       File ciphertext = new File("F:\\2.doc");
       rsa.encrypt(plaintext,ciphertext);
       
       File plainAnother = new File("F:\\3.doc");
       rsa.decrypt(ciphertext,plainAnother);
      }
    }