记录异或图片解密

异或介绍:
异或是一种基于二进制的位运算,用符号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);
                }
        }
    }
}

密钥:用原图 再加 加密后图片,可得出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值