javascript js web前端DES 加密,java后端DES解密,等保http请求明文传输漏洞处理

由于等保要求,前端手机号码、身份证号、密码等敏感数据http请求不允许明文传输,
记录一下前端DES 加密,后端DES解密的方法

javascript js 前端DES 加密

引入js

可选择cdn引入,或者本地项目引入,如果项目是内网访问,只能使用本地本地项目引入

<!--<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.0/aes.js"></script>-->
	<script src="js/crypto/crypto-js.js"></script>
	<script src="js/crypto/aes.js"></script>

前端加密

封装前端加密函数
   /**
	 * 
	 * @param value 需要加密的数据
	 * @param key  加密key
	 * @returns {string}
	 */
	function encryptByDES(value, key) {
		if(value == '') return '';
		var keyHex = CryptoJS.enc.Utf8.parse(key);
		var encrypted = CryptoJS.DES.encrypt(value, keyHex, {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		});
		return encrypted.toString();
	}
调用前端加密函数
let  str1=  encryptByDES("362421199001011111", "e45a03bf082b1c0ba1a53e14c0a393c4");
java 后台解密
封装后端加密工具类
package work.com.bsoft.utils;

import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: DESUtils
 * @Description: TODO
 * @Author: wying
 * @Date: 2024/08/19
 * @Version: V1.0
 **/
public class DESUtils {

    //定义加密算法,有DES、DESede(即3DES)、Blowfish
    private static final String ALGORITHM = "DESede";
    // 算法名称/加密模式/填充方式
    private static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";

    private static Cipher cipher = null;

    private DESUtils(){}

    private static Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
        if(cipher == null ){
            cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
        }
        return cipher;
    }

    /**
     * 加密方法
     * @param src
     * @param key
     * @return
     */
    public static String encrypt(String src, String key, boolean hex) {
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(key), ALGORITHM);    //生成密钥
            Cipher c = getCipher();    //获取Cipher工具类
            c.init(Cipher.ENCRYPT_MODE, deskey);    //初始化为加密模式
            if(hex) {
                return bytesToHexString(c.doFinal(src.getBytes()));
            }else{
                return new sun.misc.BASE64Encoder().encode(c.doFinal(src.getBytes()));
            }
        } catch (Exception e) {
        }
        return StringUtils.EMPTY;
    }

    /**
     * byte数组转换为16进制字符
     * @param bArray
     * @return
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuilder sb = new StringBuilder(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * Base64解码
     * @param src
     * @return
     */
    public static byte[] base64Decode(String src) throws IOException {
        return new BASE64Decoder().decodeBuffer(src);
    }

    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || "".equals(hexString)) {
            return new byte[0];
        }
        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]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    /**
     * char 转换 byte
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 解密函数
     * @param src 密文的字节数组
     * @param key 密钥
     * @return
     */
    public static byte[] decrypt(byte[] src, String key) {
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(key), ALGORITHM);
            Cipher c = getCipher(); //获取Cipher工具类
            c.init(Cipher.DECRYPT_MODE, deskey);    //初始化为解密模式
            return c.doFinal(src);
        } catch (Exception e) {
        }
        return new byte[0];
    }

    /**
     * 根据字符串生成密钥字节数组
     * @param keyStr 密钥字符串
     * @return
     * @throws UnsupportedEncodingException
     */
    public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
        byte[] key = new byte[24];    //声明一个24位的字节数组,默认里面都是0
        byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组

        ///执行数组拷贝
        if (key.length > temp.length) {
            //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, temp.length);
        } else {
            //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    }

    public static String decryptToString(String src, String key) throws IOException {
        if(StringUtils.isBlank(src) || StringUtils.isBlank(key)) return "";

        byte[] decrypted = decrypt(new BASE64Decoder().decodeBuffer(src), key);
        return decrypted != null?new String(decrypted):"";
    }

//    public static void main(String[] args) throws Exception {
//        System.out.println(decryptToString("L3qfqTIkBfwYexfp9F2+hQ==", Constants.LOGIN_ENCRYPT_KEY));
//    }
}

调用后端解密函数
 String str= DESUtils.decryptToString(str1, "e45a03bf082b1c0ba1a53e14c0a393c4");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值