关于身份证图片加密安全技术

前言

现在的图片都是上传到c d n或者其它第三方服务器上,通过一个url进行访问,非常的方便,方便的同时也带来了另外一个问题,隐私安全问题,比如:好莱坞隐私照片泄漏。

如何保证图片安全

如果发生客户隐私图片的泄漏,将是非常严重的事情,会使当事人遭受到骚扰、企业遭受到质疑,那么如何保证用户上传图片的安全将是一件值得重视的事情,本篇介绍一种加密方式:异或加密。

算法原理

异或的运算方法是一个二进制运算:
1^1=0
0^0=0
1^0=1
0^1=1

两者相等为0,不等为1。

对于一个字符来说,都可以用二进制码来表示。如A:01000001
字符的异或就是对每一位进行二进制运算。

用于加密算法时,假设你要加密的内容为A,密钥为B,则可以用异或加密:
C=A^B
在数据中保存C就行了。
用的时候:
A=B^C
即可取得原加密的内容,所以只要知道密钥,就可以完成加密和解密。

代码实现

文件上传工具类
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Copyright (c) 2011-2021 <br>
 * Company: 公众号:超漫时光 <br>
 * Description:  <br>
 *
 * @author cool_summer_moon <br>
 * @date 2021/3/16 9:30 上午<br>
 */
public class FileUtil {

    private static int dataOfFile = 0; // 文件字节内容
    private static String key = "coolsummermoon&^%$$^Q**";
    private static int[] array; //存放每个hash值的数组

    public static void main(String[] args) {

        File srcFile = new File("/Users/coolsummermoon/Documents/C100.jpg"); // 初始文件
        File encFile = new File("/Users/coolsummermoon/Documents/C101.jpg"); // 加密文件
        File decFile = new File("/Users/coolsummermoon/Documents/C102.jpg"); // 解密文件

        array = string2ASCII(getMD5(key));

        try {
            EncFile(srcFile, encFile); //加密操作
            DecFile(encFile, decFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void EncFile(File srcFile, File encFile) throws Exception {
        if (!srcFile.exists()) {
            System.out.println("source file not exixt");
            return;
        }

        if (!encFile.exists()) {
            System.out.println("encrypt file created");
            encFile.createNewFile();
        }
        InputStream fis = new FileInputStream(srcFile);
        OutputStream fos = new FileOutputStream(encFile);

        int i = 0;
        while ((dataOfFile = fis.read()) > -1) {

            fos.write(dataOfFile ^ array[i++]);
            if (i == array.length - 1) {
                i = 0;
            }
        }
        System.out.println(i + "");

        fis.close();
        fos.flush();
        fos.close();
    }

    // 4176587
    private static void DecFile(File encFile, File decFile) throws Exception {
        if (!encFile.exists()) {
            System.out.println("encrypt file not exixt");
            return;
        }

        if (!decFile.exists()) {
            System.out.println("decrypt file created");
            decFile.createNewFile();
        }

        InputStream fis = new FileInputStream(encFile);
        OutputStream fos = new FileOutputStream(decFile);

        int i = 0;
        while ((dataOfFile = fis.read()) > -1) {

            fos.write(dataOfFile ^ array[i++]);
            if (i == array.length - 1) {
                i = 0;
            }
        }
        System.out.println(i + "");

        fis.close();
        fos.flush();
        fos.close();
    }

    //String2Ascii
    public static int[] string2ASCII(String s) {// 字符串转换为ASCII码
        if (s == null || "".equals(s)) {
            return null;
        }

        char[] chars = s.toCharArray();
        int[] asciiArray = new int[chars.length];

        for (int i = 0; i < chars.length; i++) {
            asciiArray[i] = char2ASCII(chars[i]);
        }
        return asciiArray;
    }

    public static int char2ASCII(char c) {
        return (int) c;
    }

    public static String getMD5(String sInput) {

        String algorithm = "";
        if (sInput == null) {
            return "null";
        }
        try {
            algorithm = System.getProperty("MD5.algorithm", "MD5");
        } catch (SecurityException se) {
        }
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte buffer[] = sInput.getBytes();

        for (int count = 0; count < sInput.length(); count++) {
            md.update(buffer, 0, count);
        }
        byte bDigest[] = md.digest();
        BigInteger bi = new BigInteger(bDigest);
        return (bi.toString(16));
    }
}

结束语

异或的图片加密方式密钥越复杂、密钥的储存越安全,加密的图片就越安全。如果本篇内容对你有帮助,请点个赞,再加个关注。
来自夏夜凉月的博客截图
微信扫码,关注一位有故事的程序员。关注后(回复:1024),领取海量Java架构进阶资料。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值