import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class InformationEncryption {
public static void main(String arg[]) {
char[] str2 = "today 10pm attack destination".toCharArray();
char[] str = "晚上十点对目标发起攻击!".toCharArray();
byte[] buffer = new byte[str.length];
try {
for (int i = 0; i < str.length; i++) {
str[i] = (char) (str[i] ^ 'S');
// buffer[i] = (byte) str[i];
}
File f = new File("文档密文.txt");
FileWriter fw = new FileWriter(f);
fw.write(str, 0, str.length);
fw.close();
} catch (IOException e) {
System.out.println("" + e);
}
}
}
————————————————————————————
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class InformationDeciphering {
public static void main(String[] args) {
File f = new File("文档密文.txt");
int n = 0;
char tom[] = new char[5];
try {
FileReader fr = new FileReader(f);
System.out.println("密文:");
while ((n = fr.read(tom, 0, tom.length)) != -1) {
String s = new String(tom, 0, n);
System.out.println(s);
}
} catch (IOException e) {
System.out.println(e);
}
try {
FileReader fr2 = new FileReader(f);
System.out.println("明文:");
while ((n = fr2.read(tom, 0, tom.length)) != -1) {
for (int i = 0; i < tom.length; i++) {
tom[i] = (char) (tom[i] ^ 'S');
}
String s = new String(tom, 0, n);
System.out.println(s);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
/*结果显示:
密文:
昉乙匒烪宪
皽桔厂贤敨
冨r
明文:
晚上十点对
目标发起攻
击!*/
//对一个字符A与字符B异或后,再异或得到的还是原来的字符A;
//对于四种情况进行分析:
//A:0,B:0,一次与B异或后A:0,二次与B异或后A:0;
//A:0,B:1,一次与B异或后A:1,二次与B异或后A:0;
//A:1,B:0,一次与B异或后A:1,二次与B异或后A:1;
//A:1,B:1,一次与B异或后A:0,二次与B异或后A:1;