DES加解密工具类

DES(Data Encryption Standard)算法是常用的分组对称加密算法,实际使用中,信息传递的双方共用一个约定好的密钥进行加密和解密,原理不介绍了,直接上工具类:

package com.common.utils.des;
import java.security.*;
import javax.crypto.*;
 
/**
* @ClassName: DES
* @Description:DES加解密算法
* @author weny.yang
* @date Mar 25, 2021
*/
public class DES {
 
	private static String strDefaultKey = "12345678";    //默认的密钥
	private Cipher encryptCipher = null;
	private Cipher decryptCipher = null;
 
	/**
	 * 构造函数 DES.使用默认密钥
	 * @throws Exception
	 */
	public DES() throws Exception {
		this(strDefaultKey);
	}
 
 
	/**
	 * 构造函数 DES.使用指定的密钥
	 * @throws Exception
	 */
	public DES(String strKey) throws Exception {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		Key key = getKey(strKey.getBytes());
		encryptCipher = Cipher.getInstance("DES");
		encryptCipher.init(Cipher.ENCRYPT_MODE, key);
		decryptCipher = Cipher.getInstance("DES");
		decryptCipher.init(Cipher.DECRYPT_MODE, key);
	}
 
 
	/**
	* @Title: encrypt
	* @Description:加密字符串
	* @param strIn 需加密的字符串
	* @return 加密后的字符串
	* @author weny.yang
	* @date Mar 25, 2021
	*/
	public String encrypt(String strIn) throws Exception {
		return byteArr2HexStr(encrypt(strIn.getBytes()));
	}
	
	
	/**
	 * 加密字节数组
	 * @param arrB 需加密的字节数组
	 * @return 加密后的字节数组
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	public byte[] encrypt(byte[] arrB) throws Exception {
		return encryptCipher.doFinal(arrB);
	}
 
	
	
	/**
	 * 解密字符串
	 * @param strIn 需解密的字符串
	 * @return 解密后的字符串
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	public String decrypt(String strIn) throws Exception {
		return new String(decrypt(hexStr2ByteArr(strIn)));
	}
	
	
	/**
	 * 解密字节数组
	 * @param arrB 需解密的字节数组
	 * @return 解密后的字节数组
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	public byte[] decrypt(byte[] arrB) throws Exception {
		return decryptCipher.doFinal(arrB);
	}
 
 
	
	/**
	 * 从指定字符串生成密钥,密钥所需的字节数组长度为8位
	 * 不足8位时后面补0,超出8位只取前8位
	 * @param arrBTmp 构成该字符串的字节数组
	 * @return 生成的密钥
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	private Key getKey(byte[] arrBTmp) throws Exception {
		byte[] arrB = new byte[8];
		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}
		Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
		return key;
	}
 
	
	
	/**
	 * 将byte数组转换为表示16进制值的字符串,
	 * 如:byte[]{8,18}转换为:0813,
	 * 和public static byte[] hexStr2ByteArr(String strIn)
	 * 互为可逆的转换过程
	 * @param arrB 需要转换的byte数组
	 * @return 转换后的字符串
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	public static String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}
 
	
 
	/**
	 * 将表示16进制值的字符串转换为byte数组,
	 * 和public static String byteArr2HexStr(byte[] arrB)
	 * 互为可逆的转换过程
	 * @param strIn 需要转换的字符串
	 * @return 转换后的byte数组
	 * @author weny.yang
	 * @date Mar 25, 2021
	 * @throws Exception
	 */
	public static byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;
		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;
	}
}
package com.common.utils.des;


/**
* @ClassName: EncryUtil
* @Description:加密解密工具类
* @author weny.yang
* @date Mar 25, 2021
*/
public class EncryUtil {
	
	/**
	* @Title: encrypt
	* @Description:使用默认密钥进行DES加密
	* @author weny.yang
	* @date Mar 25, 2021
	*/
	public static String encrypt(String plainText) {
		try {
			return new DES().encrypt(plainText);
		} catch (Exception e) {
			return null;
		}
	}
 
	
	/**
	* @Title: encrypt
	* @Description:使用指定密钥进行DES解密
	* @author weny.yang
	* @date Mar 25, 2021
	*/
	public static String encrypt(String plainText, String key) {
		try {
			return new DES(key).encrypt(plainText);
		} catch (Exception e) {
			return null;
		}
	}
	
 
	/**
	* @Title: decrypt
	* @Description:使用默认密钥进行DES解密
	* @author weny.yang
	* @date Mar 25, 2021
	*/
	public static String decrypt(String plainText) {
		try {
			return new DES().decrypt(plainText);
		} catch (Exception e) {
			return null;
		}
	}
 
	/**
	* @Title: decrypt
	* @Description:使用指定密钥进行DES解密
	* @author weny.yang
	* @date Mar 25, 2021
	*/
	public static String decrypt(String plainText, String key) {
		try {
			return new DES(key).decrypt(plainText);
		} catch (Exception e) {
			return null;
		}
	}
 
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出Java中提供了3DES加解密工具类,同时支持ECB和CBC两种加密模式。如果需要使用3DESCBC解密工具类,可以按照以下步骤进行操作: 1. 导入相关的Java类库。 2. 创建3DESCBC解密工具类的实例。 3. 调用解密方法,传入密文和密钥等参数。 4. 获取解密后的明文数据。 下面是一个简单的示例代码: ``` import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class Java3DESCBCDecryptUtil { private static final String ALGORITHM = "DESede"; private static final String TRANSFORMATION = "DESede/CBC/PKCS5Padding"; private static final String CHARSET = "UTF-8"; public static String decrypt(String key, String iv, String encryptedData) throws Exception { byte[] keyBytes = key.getBytes(CHARSET); byte[] ivBytes = iv.getBytes(CHARSET); byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getBytes(CHARSET)); SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, CHARSET); } } ``` 在上面的代码中,我们使用了Java中的Cipher类来进行解密操作。同时,我们还需要提供密钥和初始化向量等参数。在实际使用中,我们可以根据具体的需求来修改代码中的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值