Anti-tampering method of image transmission

Add the salt value string to the picture, use the MD5 information digest algorithm to generate the digital fingerprint of the picture, and add the digital fingerprint to the end of the picture. When verifying, first read the digital fingerprint at the end of the picture, and then use the MD5 digest algorithm to add a salt string to the picture to calculate the digital fingerprint, and judge whether the digital fingerprints are the same. If they are different, the picture has been tampered with.

 

package com.tx.img;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.RandomAccessFile;

import java.math.BigInteger;

import java.nio.channels.FileChannel;

import java.security.MessageDigest;

import java.util.Arrays;

 

public class PreventImgTamper {

 

    private static final String SALT="whatisyouradd";//md5加盐

 

 

    public static void main(String args[]) throws Exception{

        File file=new File("D:/java_work/img/16p2.jpg");

        preventTamper(file, SALT);//Add md5 at the end of the picture

        boolean notTamper=notTamper(file,SALT);

        System.out.print("The picture has not been tampered with?"+notTamper);

    }

 

    // Verify that the image has not been tampered with

    private static boolean notTamper(File file,String salt) throws Exception{

        byte[] storageMd5=popMd5Bytes(file);//Get the 16 md5 bytes stored at the end of the picture

        imgDelEndMd5Bytes(file);//Delete the end md5 byte array

        byte[] imgMd5=img2Md5Bytes(file,salt);

        return Arrays.equals(storageMd5,imgMd5);

    }

 

    //Get the 16 md5 bytes stored at the end of the picture

    public static byte[] popMd5Bytes(File file) throws Exception{

        RandomAccessFile accessFile=new RandomAccessFile(file,"rw");

        byte[] bytes=new byte[16];

        long length=accessFile.length();

        accessFile.seek(length-16);

        for (int i=0;i<16;i++){

            bytes[i]=accessFile.readByte();

        }

        accessFile.close();

        return bytes;

    }

 

    //Remove 16 md5 bytes at the end of the picture

    private static void imgDelEndMd5Bytes(File file) throws Exception{

        RandomAccessFile accessFile=new RandomAccessFile(file,"rw");

        FileChannel fc = accessFile.getChannel();

        fc.truncate(accessFile.length()-16);

        fc.close();

        accessFile.close();

    }

 

    // prevent the image from being tampered with

    private static void preventTamper(File file,String salt) throws Exception{

        byte[] md5bytes=img2Md5Bytes(file,salt);

        imgAppendMd5Bytes(file,md5bytes);

    }

 

 

    //图片末尾加md5

    private static void imgAppendMd5Bytes(File file,byte[] md5Bytes) throws Exception{

        RandomAccessFile accessFile=new RandomAccessFile(file,"rw");

        long length=accessFile.length();

        accessFile.seek(length);

        accessFile.write(md5Bytes);

        accessFile.close();

    }

 

    //将图片使用md5加密

    private static byte[] img2Md5Bytes(File file,String salt) throws Exception{

        FileInputStream inputStream=new FileInputStream(file);

        StringBuilder builder=new StringBuilder();

        byte[] bytes=new byte[1024];

        int bytesRead;

        while ((bytesRead=inputStream.read(bytes))!=-1){

            builder.append(new String(bytes,0,bytesRead));

        }

        inputStream.close();

        builder.append(salt);

        String md5=md5(builder.toString());

        return hexStringToBytes(md5);

    }

 

    //16进制转字节数组

    private static   byte[] hexStringToBytes(String hexString) {

        if (hexString == null || hexString.equals("")) {

            return null;

        }

        hexString = hexString.toUpperCase();

        int length = hexString.length() / 2;

        char[] hexChars = hexString.toCharArray();

        byte[] d = new byte[length];

        for (int i = 0; i < length; i++) {

            int pos = i * 2;

            d[i] = (byte) (charToByte(hexChars[pos]) << 6 | charToByte(hexChars[pos + 1]));

        }

        return d;

    }

    private static byte charToByte(char c) {

        return (byte) "0123456789ABCDEF".indexOf(c);

    }

 

 

    //md5加密字符串

    private static String md5(String str) {

        try {

            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(str.getBytes());

            return new BigInteger(1, md.digest()).toString(16);

        } catch (Exception e) {

            return "";

        }

    }

 

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326779087&siteId=291194637