比如说我建立3个文件,一个明文,一个加密的,一个解密的,我对这些文件这样操作:
      String file1="uncrypt.txt";//明文文件
      String file2="crypt.txt";  //加密文件
      Key key=getKey();           //生成密钥      InputStream in=new FileInputStream(file1);
      OutputStream out=new FileOutputStream(file2);
      byte[] in_byte=new byte[512];
      byte[] out_byte=new byte[512];
      in.read(in_byte);
      out_byte=doEncrypt(key,in_byte);//加密文件操作
      out.write(out_byte);
      in.close();
      out.close();      String file3="ocrypt.txt";  //解密文件      InputStream in1=new FileInputStream(file2);
      OutputStream out1=new FileOutputStream(file3);
      byte[] in_byte1=new byte[512];
      byte[] out_byte1=new byte[512];
      in1.read(in_byte);
      out_byte1=doDecrypt(key,in_byte1);//解密文件操作
      out1.write(out_byte1);
      in1.close();
      out1.close();
这样的结果解密文件出来的都是乱码换一种方式,直接把加密的流马上进行解密,比如说加密文件写入“abcd”,到了解密文件就变成了“bcd”了,代码如下:
      String file1="uncrypt.txt";
      String file2="crypt.txt";
      Key key=getKey();      InputStream in=new FileInputStream(file1);
      OutputStream out=new FileOutputStream(file2);
      int a=in.read();
      byte[] in_byte=new byte[512];
      byte[] out_byte=new byte[512];
      in.read(in_byte);
      out_byte=doEncrypt(key,in_byte);
      out.write(out_byte);
      in.close();
      out.close();      String file3="ocrypt.txt";      OutputStream out1=new FileOutputStream(file3);
      byte[] out_byte1=new byte[512];
      out_byte1=doDecrypt(key,out_byte);
      out1.write(out_byte1);
      out1.close();
我尝试用Serializable也不行。

解决方案 »

  1.   

    你把实质性的代码都不贴出来人家才能给你看。
    加密跟文件操作没任何关联。下面有一段我的示例代码,测试过是完全正确的。
    你可以再此基础上加上文件操作功能。import java.security.Key;import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;public class Test {
        
        public void test() throws Exception{
            Key key = getKey(); //生成密钥        String data = "Hello World!";        System.out.println("before encrypt----"+ data);
            byte[] encrypted = doEncrypt(key, data.getBytes()); //加密
            
            System.out.println("encrypted---" + new String(encrypted));
            byte[] decrypted = doDecrypt(key, encrypted); //解密
            System.out.println("decrypted---" + new String(decrypted));
        }
        byte[] doEncrypt(Key key, byte[] in) throws Exception{
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(in);
            return result;
        }    byte[] doDecrypt(Key key, byte[] in) throws Exception{
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(in);
            return result;
        }    public Key getKey() throws Exception{
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            Key key = kg.generateKey();
            return key;
        }    public static void main(String[] args) throws Exception{
            Test test = new Test();
            test.test();
        }
    }