content: package com.admin4j.common.crypto;
  
  import com.admin4j.common.exception.EncryptException;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
  import java.nio.charset.StandardCharsets;
  import java.util.Base64;
  
  /**
   * des算法加解密
   * DES/ECB/PKCS5Padding
   *
   * @author andanyang
   * @since 2023/9/19 10:04
   */
  public class DesUtil {
  
      /**
       * AES
       */
      static final String ALGORITHM = "DES";
      /**
       * AES 算法
       */
      static final String DES_ECB_CIPHER = "DES/ECB/PKCS5Padding";
  
      /**
       * 加密
       *
       * @param data 需要加密的内容
       * @param key  加密密码
       * @return
       */
      public static byte[] encrypt(byte[] data, byte[] key) {
          try {
              //生成KEY
              SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
              Cipher cipher = Cipher.getInstance(DES_ECB_CIPHER);
              cipher.init(Cipher.ENCRYPT_MODE, secretKey);
              return cipher.doFinal(data);
          } catch (Exception e) {
              throw new EncryptException("encrypt error", e);
          }
      }
  
      /**
       * 解密
       *
       * @param data 待解密内容
       * @param key  解密密钥
       * @return
       */
      public static byte[] decrypt(byte[] data, byte[] key) {
          try {
              SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
              Cipher cipher = Cipher.getInstance(DES_ECB_CIPHER);
              cipher.init(Cipher.DECRYPT_MODE, secretKey);
              return cipher.doFinal(data);
          } catch (Exception e) {
              throw new EncryptException("decrypt error", e);
          }
      }
  
      /**
       * 加密
       *
       * @param data 需要加密的内容
       * @param key  加密密码
       * @return
       */
      public static String encrypt(String data, String key) {
          byte[] valueByte = encrypt(data.getBytes(StandardCharsets.UTF_8), key.getBytes(StandardCharsets.UTF_8));
          return Base64.getEncoder().encodeToString(valueByte);
      }
  
      /**
       * 解密
       *
       * @param data 待解密内容 base64 字符串
       * @param key  解密密钥
       * @return
       */
      public static String decrypt(String data, String key) {
          byte[] originalData = Base64.getDecoder().decode(data.getBytes());
          byte[] valueByte = decrypt(originalData, key.getBytes(StandardCharsets.UTF_8));
          return new String(valueByte);
      }
  }
  