前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于身份证图片加密安全技术[通俗易懂]

关于身份证图片加密安全技术[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-31 16:58:10
1.1K0
发布2022-08-31 16:58:10
举报

大家好,又见面了,我是你们的朋友全栈君。

前言

现在的图片都是上传到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 即可取得原加密的内容,所以只要知道密钥,就可以完成加密和解密。

代码实现

文件上传工具类

代码语言:javascript
复制
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));
    }
}

结束语

异或的图片加密方式密钥越复杂、密钥的储存越安全,加密的图片就越安全。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/151974.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • 如何保证图片安全
      • 算法原理
        • 代码实现
        • 结束语
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档