1 package com.copy.encrypt;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.OutputStream;
  9 import java.io.RandomAccessFile;
 10 public class FileEncryptAndDecrypt {
 11   /**
 12    * 文件file进行加密
 13    * @param fileUrl 文件路径
 14    * @param key 密码
 15    * @throws Exception
 16    */
 17   public static void encrypt(String fileUrl, String key) throws Exception {
 18     File file = new File(fileUrl);
 19     String path = file.getPath();
 20     if(!file.exists()){
 21       return;
 22     }
 23     int index = path.lastIndexOf("\\");
 24     String destFile = path.substring(0, index)+"\\"+"abc";
 25     File dest = new File(destFile);
 26     InputStream in = new FileInputStream(fileUrl);
 27     OutputStream out = new FileOutputStream(destFile);
 28     byte[] buffer = new byte[1024];
 29     int r;
 30     byte[] buffer2=new byte[1024];
 31     while (( r= in.read(buffer)) > 0) {
 32         for(int i=0;i<r;i++)
 33         {
 34           byte b=buffer[i];
 35           buffer2[i]=b==255?0:++b;
 36         }
 37         out.write(buffer2, 0, r);
 38         out.flush();
 39     }
 40     in.close();
 41     out.close();
 42     file.delete();
 43     dest.renameTo(new File(fileUrl));
 44     appendMethodA(fileUrl, key);
 45     System.out.println("加密成功");
 46   }
 47   /**
 48    *
 49    * @param fileName
 50    * @param content 密钥
 51    */
 52    public static void appendMethodA(String fileName, String content) {
 53       try {
 54         // 打开一个随机访问文件流,按读写方式
 55         RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
 56         // 文件长度,字节数
 57         long fileLength = randomFile.length();
 58         //将写文件指针移到文件尾。
 59         randomFile.seek(fileLength);
 60         randomFile.writeBytes(content);
 61         randomFile.close();
 62       } catch (IOException e) {
 63         e.printStackTrace();
 64       }
 65    }
 66    /**
 67    * 解密
 68    * @param fileUrl 源文件
 69    * @param tempUrl 临时文件
 70    * @param ketLength 密码长度
 71    * @return
 72    * @throws Exception
 73    */
 74    public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
 75       File file = new File(fileUrl);
 76       if (!file.exists()) {
 77         return null;
 78       }
 79       File dest = new File(tempUrl);
 80       if (!dest.getParentFile().exists()) {
 81         dest.getParentFile().mkdirs();
 82       }
 83       InputStream is = new FileInputStream(fileUrl);
 84       OutputStream out = new FileOutputStream(tempUrl);
 85       byte[] buffer = new byte[1024];
 86       byte[] buffer2=new byte[1024];
 87       byte bMax=(byte)255;
 88       long size = file.length() - keyLength;
 89       int mod = (int) (size%1024);
 90       int div = (int) (size>>10);
 91       int count = mod==0?div:(div+1);
 92       int k = 1, r;
 93       while ((k <= count && ( r = is.read(buffer)) > 0)) {
 94         if(mod != 0 && k==count) {
 95           r = mod;
 96         }
 97         for(int i = 0;i < r;i++)
 98         {
 99           byte b=buffer[i];
100           buffer2[i]=b==0?bMax:--b;
101         }
102         out.write(buffer2, 0, r);
103         k++;
104       }
105       out.close();
106       is.close();
107       return tempUrl;
108     }
109    /**
110    * 判断文件是否加密
111    * @param fileName
112    * @return
113    */
114    public static String readFileLastByte(String fileName, int keyLength) {
115      File file = new File(fileName);
116      if(!file.exists())return null;
117      StringBuffer str = new StringBuffer();
118       try {
119         // 打开一个随机访问文件流,按读写方式
120         RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
121         // 文件长度,字节数
122         long fileLength = randomFile.length();
123         //将写文件指针移到文件尾。
124         for(int i = keyLength ; i>=1 ; i--){
125           randomFile.seek(fileLength-i);
126           str.append((char)randomFile.read());
127         }
128         randomFile.close();
129         return str.toString();
130       } catch (IOException e) {
131         e.printStackTrace();
132       }
133       return null;
134      }
135 }

 

package com.copy.encrypt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
  /**
   * 文件file进行加密
   * @param fileUrl 文件路径
   * @param key 密码
   * @throws Exception
   */
  public static void encrypt(String fileUrl, String key) throws Exception {
    File file = new File(fileUrl);
    String path = file.getPath();
    if(!file.exists()){
      return;
    }
    int index = path.lastIndexOf("\\");
    String destFile = path.substring(0, index)+"\\"+"abc";
    File dest = new File(destFile);
    InputStream in = new FileInputStream(fileUrl);
    OutputStream out = new FileOutputStream(destFile);
    byte[] buffer = new byte[1024];
    int r;
    byte[] buffer2=new byte[1024];
    while (( r= in.read(buffer)) > 0) {
        for(int i=0;i<r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==255?0:++b;
        }
        out.write(buffer2, 0, r);
        out.flush();
    }
    in.close();
    out.close();
    file.delete();
    dest.renameTo(new File(fileUrl));
    appendMethodA(fileUrl, key);
    System.out.println("加密成功");
  }
  /**
   *
   * @param fileName
   * @param content 密钥
   */
   public static void appendMethodA(String fileName, String content) {
      try {
        // 打开一个随机访问文件流,按读写方式
        RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
        // 文件长度,字节数
        long fileLength = randomFile.length();
        //将写文件指针移到文件尾。
        randomFile.seek(fileLength);
        randomFile.writeBytes(content);
        randomFile.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }
   /**
   * 解密
   * @param fileUrl 源文件
   * @param tempUrl 临时文件
   * @param ketLength 密码长度
   * @return
   * @throws Exception
   */
   public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
      File file = new File(fileUrl);
      if (!file.exists()) {
        return null;
      }
      File dest = new File(tempUrl);
      if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
      }
      InputStream is = new FileInputStream(fileUrl);
      OutputStream out = new FileOutputStream(tempUrl);
      byte[] buffer = new byte[1024];
      byte[] buffer2=new byte[1024];
      byte bMax=(byte)255;
      long size = file.length() - keyLength;
      int mod = (int) (size%1024);
      int div = (int) (size>>10);
      int count = mod==0?div:(div+1);
      int k = 1, r;
      while ((k <= count && ( r = is.read(buffer)) > 0)) {
        if(mod != 0 && k==count) {
          r = mod;
        }
        for(int i = 0;i < r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==0?bMax:--b;
        }
        out.write(buffer2, 0, r);
        k++;
      }
      out.close();
      is.close();
      return tempUrl;
    }
   /**
   * 判断文件是否加密
   * @param fileName
   * @return
   */
   public static String readFileLastByte(String fileName, int keyLength) {
     File file = new File(fileName);
     if(!file.exists())return null;
     StringBuffer str = new StringBuffer();
      try {
        // 打开一个随机访问文件流,按读写方式
        RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
        // 文件长度,字节数
        long fileLength = randomFile.length();
        //将写文件指针移到文件尾。
        for(int i = keyLength ; i>=1 ; i--){
          randomFile.seek(fileLength-i);
          str.append((char)randomFile.read());
        }
        randomFile.close();
        return str.toString();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null;
     }
}
posted on 2018-09-20 11:55  来了^O^老弟  阅读(299)  评论(0编辑  收藏  举报