文本文件的加密,解密(其它类似)

来源:互联网 发布:xp网络共享文件夹 编辑:程序博客网 时间:2024/05/09 17:54

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 文本文件的加密,解密
 *
 * @author user
 *
 */
public class TextFileTest {
 public static void main(String[] args) throws Exception {
  TextFileTest f = new TextFileTest();
  f.encryption("C:/Documents and Settings/user/桌面/a.txt",
    "D:/yinyaogen/homework/MyJavaSample/src/day10/Day10.txt");
  f.decryption("C:/Documents and Settings/user/桌面/b.txt",
    "C:/Documents and Settings/user/桌面/a.txt");
 }

 /**
  * 加密
  */
 public void encryption(String url1, String url2) throws Exception {
  FileOutputStream fileOutputStream = new FileOutputStream(url1);
  FileInputStream fileInputStream = new FileInputStream(url2);
  // byte[] arr = new byte[1024];
  // int i = fileInputStream.read(arr);
  int i = ~fileInputStream.read();
  byte[] arrByte = new byte[1024];
  int index = 0;
  while (i != -1) {
   char c = (char) i;
   if (c < 256) {
    c += 30;
    i = c+30;
   }
   arrByte[index++] = (byte) i;
   while (index == 1024) {
    fileOutputStream.write(arrByte, 0, arrByte.length);
    index = 0;
   }
   i = fileInputStream.read();
   if(i == -1)
    fileOutputStream.write(arrByte, 0, index-1);
  }
  System.out.println(2*2*2*2*2*2*2*2);
  fileInputStream.close();
  fileOutputStream.close();
 }

 /**
  * 解密
  */
 public void decryption(String url1, String url2) throws Exception {
  FileOutputStream fileOutputStream = new FileOutputStream(url1);
  FileInputStream fileInputStream = new FileInputStream(url2);
  // byte[] arr = new byte[1024];
  int i = ~fileInputStream.read();
  byte[] arrByte = new byte[1024];
  int index = 0;
  while (i != -1) {
   char c = (char) i;
   if (c < 286) {
    c -= 30;
    i = c-30;
   }
   arrByte[index++] = (byte) i;
   while (index == 1024) {
    fileOutputStream.write(arrByte, 0, arrByte.length);
    index = 0;
   }
   i = fileInputStream.read();
   if(i == -1)
    fileOutputStream.write(arrByte, 0, index-1);
  }
  fileInputStream.close();
  fileOutputStream.close();
 }

}

原创粉丝点击