加密方法是通過輸入流對源文件字符逐個讀取,對其讀取到字符的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 }}