import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public static final byte XOR_CONST = 0X12;
public static void main(String[] args) throws Exception {
File src = new File("d://level1.doc");
File dest = new File("d://level2.doc");
File dest2 = new File("d://level3.doc");
xorEn(src, dest);
xorEn(dest, dest2);
}
/**
* 异或的一个特点: a^b = c c^b = a
* 所以简单点,这里的加解密都用一个函数就行了
* @param src
* @param dest
* @throws Exception
*/
public static void xorEn(File src, File dest) throws Exception {
// 文件不存在或为文件夹就不判断了
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] bs = new byte[1024];
int len = 0;
while ((len = fis.read(bs)) != -1) {
for (int i = 0; i < len; i++) {
bs[i] ^= XOR_CONST;
}
fos.write(bs, 0, len);
}
fos.close();
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
*需要添加一个临时文件
*/
public class XorExample {public static final byte XOR_CONST = 0X12;
public static void main(String[] args) throws Exception {
File src = new File("d://level1.doc");
File dest = new File("d://level2.doc");
File dest2 = new File("d://level3.doc");
xorEn(src, dest);
xorEn(dest, dest2);
}
/**
* 异或的一个特点: a^b = c c^b = a
* 所以简单点,这里的加解密都用一个函数就行了
* @param src
* @param dest
* @throws Exception
*/
public static void xorEn(File src, File dest) throws Exception {
// 文件不存在或为文件夹就不判断了
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] bs = new byte[1024];
int len = 0;
while ((len = fis.read(bs)) != -1) {
for (int i = 0; i < len; i++) {
bs[i] ^= XOR_CONST;
}
fos.write(bs, 0, len);
}
fos.close();
fis.close();
}
}