DES算法加密,解密工具类

该博客介绍了一个使用DES算法进行加密和解密的Java工具类。类中包含了加密后Base64编码输出、加密输出字节数组、解密到字符串以及解密字节数组的方法。所有方法都围绕着读取固定密钥、初始化Cipher实例以及执行加密或解密操作展开。在遇到异常时,日志会记录错误并抛出IllegalStateException。
摘要由CSDN通过智能技术生成
package com.zsh.commons.utils;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * DES算法加密,解密工具类
 */
public class DESUtils {

	private static final Logger logger = LoggerFactory.getLogger(DESUtils.class);

	public static SecretKey readKey() {

		try {		
			byte[] keyByte = new byte[]{91, 103, 84, 52, 110, 117, -5, -123};
			DESKeySpec ks = new DESKeySpec(keyByte);
			SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
			return key;

		} catch (InvalidKeyException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (InvalidKeySpecException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");	
		}
	}

	/**
	 * 加密后用Base64编码输出
	 */
	public static String encode2String(String text) {
		if(StringUtils.isBlank(text) || text.equalsIgnoreCase("null")){
			return "";
		}
		byte[] en = encode(text);
		return String.valueOf(Hex.encodeHex(en)).toUpperCase();
	}

	/**
	 * 加密后输出字节数组
	 */
	public static byte[] encode(String text) {

		SecretKey key = readKey();

		try {
			Cipher cipher = Cipher.getInstance("DES");

			cipher.init(Cipher.ENCRYPT_MODE, key);

			byte[] encpted = cipher.doFinal(text.getBytes());

			return encpted;

		} catch (InvalidKeyException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (NoSuchPaddingException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (IllegalBlockSizeException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (BadPaddingException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");
		}
	}

	public static String decode2String(String text) {
		if(StringUtils.isBlank(text) || text.equalsIgnoreCase("null")){
			return "";
		}
		try {
			byte[] b64_de = Hex.decodeHex(text.toCharArray());
			return decode(b64_de);		
		} catch (DecoderException e) {
			throw new IllegalArgumentException(e.getMessage());
		}
	}

	public static String decode(byte[] text) {
		SecretKey key = readKey();

		try {
			Cipher cipher = Cipher.getInstance("DES");

			cipher.init(Cipher.DECRYPT_MODE, key);

			byte[] decpted = cipher.doFinal(text);

			return new String( decpted );

		} catch (InvalidKeyException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (NoSuchPaddingException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (IllegalBlockSizeException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		} catch (BadPaddingException e) {
			logger.error(e.getMessage(), e);
			throw new IllegalStateException("无效的密钥文件");

		}
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值