【Java工具类】(30)—DES加密工具类

Java工具类(30)—DES加密工具类

package com.awifi.cloudnative.container.manage.provider.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

/**
* 加密/解密 工具类
* @author 
* @date 2021/8/6
*/
public class EncryptUtil {

	/**
    * DES加密方法
    * @param message 加密数据
    * @param keyString 密钥
    * @return 加密结果
    */
   public static String encryptByDes(String message, String keyString) {
   	if(StringUtils.isEmpty(message) || StringUtils.isEmpty(keyString)){//当 加密数据或密钥 为空时,不做加密操作
           return StringUtils.EMPTY;
       }
       String keyHexString = stringToHexString(keyString);
       byte[] key = hexStringToBytes(keyHexString);
       String dataHexString = stringToHexString(message);
       byte[] data = hexStringToBytes(dataHexString);
       try {
           SecureRandom sr = new SecureRandom();
           SecretKeyFactory keyFactory;
           DESKeySpec dks = new DESKeySpec(key);
           keyFactory = SecretKeyFactory.getInstance("DES");
           SecretKey secretkey = keyFactory.generateSecret(dks);
           Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
           cipher.init(Cipher.ENCRYPT_MODE, secretkey, sr);
           byte[] result = cipher.doFinal(data);
           return new String(Base64.encodeBase64(result));
       } catch (Exception e) {
           e.printStackTrace();
       }
       return StringUtils.EMPTY;
   }
   
   /**
    * DES解密方法
    * @param dataHexString 解密数据
    * @param keyString 密钥
    * @return 解密后的值 
    */
   public static String decryptByDes(String dataHexString, String keyString) {
   	if(StringUtils.isEmpty(dataHexString) || StringUtils.isEmpty(keyString)){//当 解密数据或密钥 为空时,不做加密操作
           return StringUtils.EMPTY;
       }
       String keyHexString = stringToHexString(keyString);
       byte[] key = hexStringToBytes(keyHexString);
       byte[] result = null;
       try {
           SecureRandom sr = new SecureRandom();
           SecretKeyFactory keyFactory;
           DESKeySpec dks = new DESKeySpec(key);
           keyFactory = SecretKeyFactory.getInstance("DES");
           SecretKey secretkey = keyFactory.generateSecret(dks);
           Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
           cipher.init(Cipher.DECRYPT_MODE, secretkey, sr);
           result = cipher.doFinal(Base64.decodeBase64(dataHexString));
           return new String(result).trim();
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }
   
   /**
    * 字符串转16进制字符串
    * @param str 字符串
    * @return 转后的字符串
    */
   public static String stringToHexString(String str) {
       char[] chars = "0123456789ABCDEF".toCharArray();
       StringBuilder sb = new StringBuilder("");
       byte[] bs = str.getBytes();
       int bit;
       for (int i = 0; i < bs.length; i++) {
           bit = (bs[i] & 0x0f0) >> 4;
           sb.append(chars[bit]);
           bit = bs[i] & 0x0f;
           sb.append(chars[bit]);
       }
       return sb.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;
   }
   
   /**
    * byte数组转换成16进制字符串
    * @param b byte数组
    * @return 转换成16进制字符串
    */
   public static String bytesToHexString(byte[] b) {
       StringBuilder stringBuilder = new StringBuilder();
       if (b == null || b.length <= 0) {
           return null;
       }
       for (int i = 0; i < b.length; i++) {
           int v = b[i] & 0xFF;
           String hv = Integer.toHexString(v);
           if (hv.length() < 2) {
               stringBuilder.append(0);
           }
           stringBuilder.append(hv);
       }
       return stringBuilder.toString();
   }
   
   /**
    * 
    * @param c 字符
    * @return byte
    */
   private static byte charToByte(char c) {
       return (byte) "0123456789ABCDEF".indexOf(c);
   }
   
   /**
    * 16进制字符串转字符串
    * @param str 字符串
    * @return 字符串
    */
   public static String hexStringToString(String str) {
       byte[] baKeyword = new byte[str.length() / 2];
       for (int i = 0; i < baKeyword.length; i++) {
           try {
               baKeyword[i] = (byte) (0xff & Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16));
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       try {
           str = new String(baKeyword, "utf-8");// UTF-16le:Not
       } catch (Exception e1) {
           e1.printStackTrace();
       }
       return str;
   }

   /**
    * md5加密
    * @param data 原始数据
    * @return 加密数据(16进制字符串)
    * @author 范立松
    * @date 2018年8月23日 上午9:06:55
    */
   public static String md5Digest(String data) {
       byte[] bytes = DigestUtils.md5Digest(data.getBytes(StandardCharsets.UTF_8));
       return byte2hex(bytes);
   }

   /**
    * byte[]转换为16进制字符串
    * @param bytes 字节数组
    * @return 16进制字符串
    * @author 范立松
    * @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();
   }

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

       // des 解密
        String decryptStr = decryptByDes(encryptStr,"50739180");
        System.out.println("解密后的字符串 = " + decryptStr);
   }
   
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值