Java实现DES加密解密工具,CBC模式和向量(偏移量)等等参数的设置以及16进制和Base64两种密文编码

  

  示例代码如下:

package com.fangyi.utils;

import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;

/**
 * @author cyl
 * @time 2022/6/10
 */
public class DESEncoderUtils {
    /**
     * @Description: 对参数进行DES和Base64加密
     */
    public static String encryptParam(String sourceParam, String key) throws Exception {
        byte[] byteContent = sourceParam.getBytes("UTF-8");
        //SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        //密钥
        SecretKey securekey = keyFactory.generateSecret(desKey);
        //向量
        AlgorithmParameterSpec iv = new IvParameterSpec(key.getBytes());
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
        //选择Base64编码或16进制编码,加密与解密必须统一对应
        //return Base64.getEncoder().encodeToString(cipher.doFinal(byteContent));
        return byte2hex(cipher.doFinal(byteContent));
    }
    //对base64或16进制以及DES加密后数据进行解密
    public static String decryptParam(String sourceParam, String key) throws Exception {
    //    byte[] byteContent = Base64.getDecoder().decode(sourceParam);
        byte[] byteContent = hexStringToBytes(sourceParam);
        //SecureRandom random = new SecureRandom();
        //向量
        AlgorithmParameterSpec iv = new IvParameterSpec(key.getBytes());
        //密钥
        DESKeySpec desKey = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
        return new String(cipher.doFinal(byteContent), "UTF-8");
    }

    /**
     * byte[]转换为16进制字符串
     * @param bytes 字节数组
     * @return 16进制字符串
     * @author cyl
     * @date 2018年8月23日 上午9:07:40
     */
    public static String byte2hex(byte[] bytes) {
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                sign.append("0");
            }
            sign.append(hex.toUpperCase());
        }
        return sign.toString();
    }

    /**
     * 16进制字符串转成byte数组
     * @param hexString 字符串
     * @return 转后的字符串
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        int hexLength = length;
        while (hexLength % 8 != 0) {
            hexLength++;
        }
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[hexLength];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     *
     * @param c 字符
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 测试函数
     * @param args 参数
     * @date 2015年11月30日 下午3:02:07
     */
    public static void main(String[] args) throws Exception {
        // des 加密
        String str = "测试";
        String encryptStr = encryptParam(str, "YHNIKUJM");
        System.out.println("加密后的字符串 = " + encryptStr);
        //  des 解密
        String decryptStr = decryptParam(encryptStr,"YHNIKUJM");
        System.out.println("解密后的字符串 = " + decryptStr);
    }


}

 

posted @ 2022-06-13 15:42  onecyl  阅读(1878)  评论(0编辑  收藏  举报