我的问题是这样的 对一个文件进行加密就是将文件的内容读取出来然后进行一个操作
再将这个操作完的字节数组重新写入这个文件 覆盖原内容
解密操作就是 也进行操作 再将这个操作完的字节数组重新写入这个文件 覆盖内容
这样得到的就是文件的原内容
代码如下:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class EndeUtil {

 // 加密KEY不能随便改动
 static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
   .getBytes();  static final int BUFFERLEN = 512;
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
   // debug =false ;    String oldFile = new String("D:\\aaa\\aa.txt");
   String newFile = new String("D:\\aaa\\aa_en.txt");
   String dFile= new String("D:\\aaa\\aa_en_d.txt");
   encryptFile(oldFile, "123456");//将原文件加密成新文件    System.out.println("ok");
     
   decryptFile(oldFile);//将加密的文件重新变成一个文件
     
   System.out.println("good");
 } catch (Exception e) {
   e.printStackTrace();
  } }

/**
 * 加密方法 将文件内容做加密 加密内容覆盖原内容
 * @param file
 * @param password
 */
public static void encryptFile(String file,String password)throws Exception{
//String newFile = new String("D:\\aaa\\aa.txt");
FileInputStream in = new FileInputStream(file);

int c, pos, keylen;
  pos = 0;
  keylen = KEYVALUE.length;
  byte buffer[] = new byte[BUFFERLEN];
  while ((c = in.read(buffer)) != -1) {
   for (int i = 0; i < c; i++) {
    buffer[i] ^= KEYVALUE[pos];
    //out.write(buffer[i]);
    pos++;
    if (pos == keylen)
     pos = 0;
   }
  }
  in.close();
  FileOutputStream out = new FileOutputStream(file);
  for(int i=0;i<buffer.length;i++){
  out.write(buffer[i]);
  }
  
  out.close();
} public static void decryptFile(String file)throws Exception{
//String newFile = new String("D:\\aaa\\aa_en.txt");
FileInputStream in = new FileInputStream(file);

 int c, pos, keylen;
  pos = 0;
  keylen = KEYVALUE.length;
  byte buffer[] = new byte[BUFFERLEN];
  while ((c = in.read(buffer)) != -1) {
   for (int i = 0; i < c; i++) {
    buffer[i] ^= KEYVALUE[pos];
   // out.write(buffer[i]);
    pos++;
    if (pos == keylen)
     pos = 0;
   }
  }
  in.close();
  FileOutputStream out = new FileOutputStream(file);
  for(int i=0;i<buffer.length;i++){
  out.write(buffer[i]);
  }
  
  out.close();
}
}这个算法是没有问题的 因为如果将文件加密的内容写入另一个文件 就可以 解密时再写入第三个文件
比较第三个文件和原文件 内容是一样的 
而我想 全部在一个文件上操作却不可以实现 数据不能完全恢复 不知道这么调整 谁来告诉我 
下面再贴出 网上找的源码import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;/**
 * 加密解密类
 * 
 * @author shaohl
 * @version 1.00
 */public class EncryptGen { // static boolean debug =false ; // 加密KEY不能随便改动
 static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
   .getBytes(); static final int BUFFERLEN = 512; public EncryptGen() {
 } /**
  * 对文件进行加密
  * 
  * @param String
  *            oldFile 原始要加密的文件
  * @param String
  *            newFile 加密后的文件
  * @return
  */
 public static void encryptFile(String oldFile, String newFile)
   throws Exception {
  FileInputStream in = new FileInputStream(oldFile);
  File file = new File(newFile);
  if (!file.exists())
   file.createNewFile();
  FileOutputStream out = new FileOutputStream(file);
  int c, pos, keylen;
  pos = 0;
  keylen = KEYVALUE.length;
  byte buffer[] = new byte[BUFFERLEN];
  while ((c = in.read(buffer)) != -1) {
   for (int i = 0; i < c; i++) {
    buffer[i] ^= KEYVALUE[pos];
    out.write(buffer[i]);
    pos++;
    if (pos == keylen)
     pos = 0;
   }
  }
  in.close();
  out.close();
 } /**
  * 对文件进行解密
  * 
  * @param String
  *            oldFile 原始要解密的文件
  * @param String
  *            newFile 解密后的文件
  * @return
  */
 public static void decryptFile(String oldFile, String newFile)
   throws Exception {
  FileInputStream in = new FileInputStream(oldFile);
  File file = new File(newFile);
  if (!file.exists())
   file.createNewFile();
  FileOutputStream out = new FileOutputStream(file);
  int c, pos, keylen;
  pos = 0;
  keylen = KEYVALUE.length;
  byte buffer[] = new byte[BUFFERLEN];
  while ((c = in.read(buffer)) != -1) {
   for (int i = 0; i < c; i++) {
    buffer[i] ^= KEYVALUE[pos];
    out.write(buffer[i]);
    pos++;
    if (pos == keylen)
     pos = 0;
   }
  }
  in.close();
  out.close();
 } /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   // debug =false ;   String oldFile = new String("D:\\aaa\\w1.doc");
   String newFile = new String("D:\\aaa\\w2.doc");
   String dFile= new String("D:\\aaa\\w3.doc");
   encryptFile(oldFile, newFile);//将原文件加密成新文件     System.out.println("ok");
     
     decryptFile(newFile, dFile);//将加密的文件重新变成一个文件
     
     System.out.println("good");
  } catch (Exception e) {
   e.printStackTrace();
  } }}

解决方案 »

  1.   

    如果是好几十兆的不是很占 空间 我用了 Byte 字节流 倒是能解决 
    但是加密的key 有问题了 
    简单的可以解开 还有一个最最最最复杂的问题  我这个要加密解密的文件时pdf的 读取pdf的字节加密后 不对啊....
    4M的内容 加密后变成1KB 明显错误...
    怎么pdf 这么特别
    这个谁能搞定啊!!!!!
      

  2.   

    你看下,可能是把pdf中的二进制的大部分00字节给舍掉了。
      

  3.   

    pdf如果是图片生成的你怎么加密的?
      

  4.   

    1.读写pdf要下载专用api  (pdfbox) 先学怎么读写 http://pdfbox.apache.org/download.html
    2.加密
      

  5.   

    我找的那个pdf是图片的比较多 
    是不是有影响?
      

  6.   

    用byte流 神马都不是问题 上面自己的代码发现byte流用错了 修改后好了