java实现对文件的一个简单的加密及解密:涉及java IO,字符间的异或运算

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;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值