异或介绍:
异或是一种基于二进制的位运算,用符号XOR或者 ^ 表示,其运算法则是对运算符两侧数的每一个二进制位,按位异或,同值取0,异值取1。
性质
1、交换律
2、结合律(即(ab)c == a(bc))
3、对于任何数x,都有xx=0,x0=x
4、自反性 A XOR B XOR B = A XOR 0 = A
例:
a ^b ^b
假如 a = 1001 b =1011
第一次 就是 0010
第二次 0010 和 1001 产出 1011
所以就是b
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.*;
public class GXImageDecrypt {
public static void main(String[] args) throws IOException {
String path = "E:\\2021";
String decryptErrorPath = "E:\\2021.txt";
final byte[] KEY = "HISENSE@KEY.FILE".getBytes();
File file = new File(path);
File decryptError = new File(decryptErrorPath);
if (!decryptError.exists()) {
decryptError.createNewFile();
}
FileWriter fileWriter = new FileWriter(decryptErrorPath, true);
decryptFiles(file, KEY,fileWriter);
}
private static void decryptFiles(File folder, byte[] KEY, FileWriter fileWriter) throws IOException {
File[] subFolders = folder.listFiles();
if (null != subFolders) {
for (File file : subFolders)
if (file.isFile()) {
try{
RandomAccessFile f = new RandomAccessFile(file, "r");
byte[] content = new byte[(int) f.length()];
f.readFully(content);
Image image = ImageIO.read(file);
if(null!=image){
continue;
}
int pos = 0;
long c = (content.length <= 5120L) ? content.length : 5120L;
int i = 0;
while (i < c) {
content[i] ^= KEY[pos++];
if (pos >= KEY.length) {
pos = 0;
}
i++;
}
RandomAccessFile r = new RandomAccessFile(file, "rw");
r.write(content);
}catch (Exception e){
fileWriter.write("解密失败文件路径:"+file.toString());
System.getProperty("line.separator");
fileWriter.write("解密失败文件路径:"+e.getLocalizedMessage());
System.getProperty("line.separator");
fileWriter.flush();
}
} else {
decryptFiles(file, KEY,fileWriter);
}
}
}
}
密钥:用原图 再加 加密后图片,可得出