java文本文件加密

加密方法是通过输入流对源文件字符逐个读取,对其读取到字符的ascll值进行异或运算,并将其放入新文件中,解密时只要用相同的密钥进行ascll异或运算并向新文件输出即可,即对文件首次用该程序处理为加密,第二次处理即为解密,代码如下:

 1 package word;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.io.PrintStream;
 9 
10 
11 public class Mi {
12     static InputStream in = null;
13     static PrintStream out = null;
14 
15    
16     public static void main(String[] args) throws IOException {
17         // TODO 自动生成的方法存根
18         
19         String path1="C:\\fille1";
20        
21         String path2="C:\\fille2";
22         
23     
24         File file = new File(path1);
25         File file2=new File(path2); 
26         key(file2,file);
27         System.out.println("完成");
28 
29              
30         }
31    
32 static void key(File a,File b) throws IOException
33 {
34     in = new FileInputStream(a);
35     out=new PrintStream(b);
36     int tempbyte;
37     while ((tempbyte =  in.read()) != -1) {
38         tempbyte=tempbyte^98;//进行异或运算来达到加密的目的
39     out.print((char)tempbyte);
40         
41 }
42 
43 
44 }}

猜你喜欢

转载自www.cnblogs.com/liuleliu/p/11831499.html