java 3DES加解密

/**

  • Bestpay.com.cn Inc.
  • Copyright © 2011-2019 All Rights Reserved.
    */
    package com.bestpay.bank.agreement.common.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;

/**

  • 3DES 加密 解密

  • @author

  • @version Id: DESUtil.java, v 0.1 2019/4/10 09:27 hujie Exp $$
    */
    public class DESUtil {

    // 3des解密
    private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };

    private final static String[] hexDigits = { “0”, “1”, “2”, “3”, “4”, “5”,
    “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E”, “F” };

    public static String desDecrypt(String cipherText, String strkey) {
    try{
    cipherText = new String(hexString2ByteArray(cipherText));
    strkey = procKey(strkey);
    Base64 base64d = new Base64();
    DESedeKeySpec p8ksp = null;
    p8ksp = new DESedeKeySpec(base64d.decode(strkey));
    Key key = null;
    key = SecretKeyFactory.getInstance(“DESede”).generateSecret(p8ksp);

         Cipher cipher = null;
         byte[] inPut = base64d.decode(cipherText);
         cipher = Cipher.getInstance("DESede/CBC/NoPadding");
         SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
         IvParameterSpec ivspec = new IvParameterSpec(myIV);
         cipher.init(2, myKey, ivspec);
         byte[] output = removePadding(cipher.doFinal(inPut));
    
         return new String(output, "UTF8");
     }catch (Exception e){
    
     }
     return null;
    

    }

    public static String desEncrypt(String input,String strkey){
    String cardNo = input;
    strkey = procKey(strkey);
    Base64 base64 = new Base64();
    DESedeKeySpec p8ksp = null;
    try {
    p8ksp = new DESedeKeySpec(base64.decode(strkey));
    Key key = null;
    key = SecretKeyFactory.getInstance(“DESede”).generateSecret(p8ksp);

         input = padding(input);
         if (null == input) {
             return cardNo;
         }
         byte[] plainBytes = (byte[]) null;
         Cipher cipher = null;
         byte[] cipherText = (byte[]) null;
    
         plainBytes = input.getBytes("UTF8");
         cipher = Cipher.getInstance("DESede/CBC/NoPadding");
         SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
         IvParameterSpec ivspec = new IvParameterSpec(myIV);
         cipher.init(1, myKey, ivspec);
         cipherText = cipher.doFinal(plainBytes);
         String regStr = removeBR(base64.encodeToString(cipherText));
         String rtn = byteArrayToHexString(regStr.getBytes());
         return rtn;
     }catch (Exception e) {
    
     }
    
     return null;
    

    }

    /**

    • 转换字节数组为16进制字串
    • @param b
    •        字节数组
      
    • @return 16进制字串
      */

    public static String byteArrayToHexString(byte[] b) {
    StringBuffer resultSb = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
    resultSb.append(byteToHexString(b[i]));
    }
    return resultSb.toString().toUpperCase();
    }

    private static String byteToHexString(byte b) {
    int n = b;
    if (n < 0)
    n = 256 + n;
    int d1 = n / 16;
    int d2 = n % 16;
    return hexDigits[d1] + hexDigits[d2];
    }

    private static String removeBR(String str) {
    StringBuffer sf = new StringBuffer(str);

     for (int i = 0; i < sf.length(); ++i)
     {
         if (sf.charAt(i) == '\n')
         {
             sf = sf.deleteCharAt(i);
         }
     }
     for (int i = 0; i < sf.length(); ++i) {
         if (sf.charAt(i) == '\r') {
             sf = sf.deleteCharAt(i);
         }
     }
    
     return sf.toString();
    

    }

    public static String padding(String str)
    {
    byte[] oldByteArray;
    try
    {
    oldByteArray = str.getBytes(“UTF8”);
    int numberToPad = 8 - oldByteArray.length % 8;
    byte[] newByteArray = new byte[oldByteArray.length + numberToPad];
    System.arraycopy(oldByteArray, 0, newByteArray, 0, oldByteArray.length);
    for (int i = oldByteArray.length; i < newByteArray.length; ++i)
    {
    newByteArray[i] = 0;
    }
    return new String(newByteArray, “UTF8”);
    }
    catch (UnsupportedEncodingException e)
    {
    }
    return null;
    }

    /**

    • 将表示16进制值的字符串转换为byte数组, 和public static String byteArrayToHexString(byte[] b)

    • 互为可逆的转换过程

    • @param strIn

    •        需要转换的字符串
      
    • @return 转换后的byte数组

    • @throws Exception

    •         本方法不处理任何异常,所有异常全部抛出
      
    • @author LiGuoQing
      */
      public static byte[] hexString2ByteArray(String strIn) throws Exception {
      byte[] arrB = strIn.getBytes();
      int iLen = arrB.length;

      // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
      byte[] arrOut = new byte[iLen / 2];
      for (int i = 0; i < iLen; i = i + 2) {
      String strTmp = new String(arrB, i, 2);
      arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
      }
      return arrOut;
      }

    /**

    • 把KEY处理成32位,如果不足,在后面补0,如果超出,截取前32位

    • */
      private static String procKey(String key) {
      if(key.length()<32) {
      while(key.length()<32) {
      key = key + “0”;
      }
      return key;
      }else if(key.length()>32) {
      return key.substring(0,32);
      }

      return key;
      }

    public static byte[] removePadding(byte[] oldByteArray)
    {
    int numberPaded = 0;
    for (int i = oldByteArray.length; i >= 0; --i)
    {
    if (oldByteArray[(i - 1)] != 0)
    {
    numberPaded = oldByteArray.length - i;
    break;
    }
    }

     byte[] newByteArray = new byte[oldByteArray.length - numberPaded];
     System.arraycopy(oldByteArray, 0, newByteArray, 0, newByteArray.length);
    
     return newByteArray;
    

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值