欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Des加密

程序员文章站 2022-06-05 09:01:33
...

des加密比较常见,使用频率比较高,

记录两种des加密方式:ECB和CBC

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

  /**
     * 加密数据
     * @param encryptString  注意:这里的数据长度只能为8的倍数
     * @param encryptKey
     * @return
     * @throws Exception
     */
    public static byte[] encryptDES(String encryptString, String encryptKey) throws Exception {
        byte[] encryptStringTemp=pading(encryptString);
        SecretKeySpec key = new SecretKeySpec(getKey(encryptKey), "DES");
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(encryptStringTemp);
        return encryptedData;
    }

   public static byte[] encryptDES_CBC(String encryptString, String encryptKey,String iv1) throws Exception {
        IvParameterSpec iv = new IvParameterSpec(getIv(iv1));
        byte[] encryptStringTemp=pading(encryptString);
        SecretKeySpec key = new SecretKeySpec(getKey(encryptKey), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,key, iv);
        byte[] encryptedData = cipher.doFinal(encryptStringTemp);
        return encryptedData;
    }


    public static byte[] getIv(String iv) throws DecoderException {
      return   Hex.decodeHex(iv.toCharArray());
    }




  private static byte[] pading(String str){
        byte[] strBs=str.getBytes();
        return pading(strBs);


    }


  private static byte[] pading(byte[] strBs){
       if (strBs.length % 8 !=0) {
           int n=strBs.length/8;
           byte[] pading=new byte[8*(n+1)];
           System.arraycopy(strBs, 0, pading, 0, strBs.length);
           //不足8位的进行补足
           for(int i=strBs.length;i<pading.length;i++){
               pading[i]=(byte)0;
           }
           return pading;
       } else {
            return strBs;
       }


  }


 /**
     * 自定义一个key
     * @param
     */
    public static byte[] getKey(String keyRule) {
        Key key = null;
        byte[] keyByte = keyRule.getBytes();
        // 创建一个空的八位数组,默认情况下为0
        byte[] byteTemp = new byte[8];
        // 将用户指定的规则转换成八位数组
        for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
            byteTemp[i] = keyByte[i];
        }
        key = new SecretKeySpec(byteTemp, "DES");
        return key.getEncoded();
    }

 /***
     * 解密数据
     * @param decryptString
     * @param decryptKey
     * @return
     * @throws Exception
     */
    public static String decryptDES(String decryptString, String decryptKey) throws Exception {
        byte[] b = ConvertUtil.hexStringToByte(decryptString);
        b = pading(b);
        SecretKeySpec key = new SecretKeySpec(getKey(decryptKey), "DES");
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte decryptedData[] = cipher.doFinal(b);
        return new String(decryptedData,"UTF-8");
    }