用异或对身份证等图片加密的严谨方式

用异或对身份证等图片加密的严谨方式

背景

最近遇到了一个上传身份证的需求, 担心用户身份证泄露,所以对用户身份证信息进行异或加密,网上的异或方式用固定的key加密, 即使拿到加密图片也很容易破解, 于是乎灵机一动,写下不容易被破解key的办法,分享给有类似诉求的朋友:

直接上代码


public class Demo {

    public static void main(String[] args) throws Exception {
        String source = "/home/xxx/develop/111.jpeg";
        String target = "/home/xxx/develop/222.jpeg";
        String decode = "/home/xxx/develop/333.jpeg";
        encode(source, target);
        encode(target, decode);
        printHead(target, 32);
    }

    public static void encode(String source, String target) throws Exception {
        int[] key = {5,4,3,2,1,1,2,3,4,5,6,7,8,9,1}; //随意设置
        System.out.println("");
        File inFile = new File(source);
        File outFile = new File(target);

        FileInputStream input = new FileInputStream(inFile);
        FileOutputStream output = new FileOutputStream(outFile);

        int i = 0;
        int j = 0;
        int content = 0;
        while ((content = input.read()) != -1) {
            i++;
            if (i <= 32) { //为了跳过文件头
                System.out.print(content + " ");
                output.write(content);
            } else {
                j++;
                int index = j % key.length;
                output.write(content ^ key[index]);
            }

        }
        //关闭资源
        output.close();
        input.close();

    }

    public static void printHead(String source, int size) throws Exception {
        System.out.println("");
        File inFile = new File(source);

        FileInputStream input = new FileInputStream(inFile);

        int i = 0;
        int content = 0;
        while ((content = input.read()) != -1 && i < size) {
            i++;
            System.out.print(content + " ");
        }

        //关闭资源
        input.close();

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值