Des加密

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");
    }




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值