Des算法通用类

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * DES 算法
 * 
 * @author Fu Wei
 * 
 */
public class DesUtils {
	
	private static final Logger LOG = LoggerFactory.getLogger(DesUtils.class);
	
	private final static String DES = "DES";
	private static final String ENCODE = "UTF-8";
	private static final String DECODE = "UTF-8";
	/**
	 * 加密String明文输入,String密文输出
	 * 
	 * @param strMing
	 *            String明文
	 * @return String密文
	 * @throws DesException
	 */
	public static String getEncString(String strMing, byte[] byteKey) {
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = null;
		try {
			byteMing = strMing.getBytes(ENCODE);
			byteMi = encrypt(byteMing, byteKey);
			strMi = Base64Utils.encode(byteMi);
		} catch (UnsupportedEncodingException e) {
			LOG.error("不支持的编码",e);
		}
		return strMi;
	}

	/**
	 * 解密 以String密文输入,String明文输出
	 * 
	 * @param strMi
	 *            String密文
	 * @return String明文
	 */
	public static String getDesString(String strMi, byte[] byteKey) {
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = null;
		try {
			byteMi = Base64Utils.decode(strMi);
			byteMing = decrypt(byteMi, byteKey);
			strMing = new String(byteMing, DECODE);
		} catch (Exception e) {
			LOG.error("获取Des错误",e);
		}
		return strMing;
	}

	/**
	 * 加密
	 * 
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return 返回加密后的数据
	 * @throws DesException
	 */
	public static byte[] encrypt(byte[] src, byte[] key) {
		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		try {
			// 从原始密匙数据创建DESKeySpec对象
			DESKeySpec dks = new DESKeySpec(key);
			// 创建一个密匙工厂,然后用它把DESKeySpec转换成
			// 一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
			SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher对象实际完成加密操作
			Cipher cipher = Cipher.getInstance(DES);
			// 用密匙初始化Cipher对象
			cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
			// 现在,获取数据并加密
			// 正式执行加密操作
			return cipher.doFinal(src);
		} catch (NoSuchAlgorithmException e) {
			LOG.error("算数错误",e);
		} catch (InvalidKeyException e) {
			LOG.error("无效key错误",e);
		} catch (InvalidKeySpecException e) {
			LOG.error("无效key戳无",e);
		} catch (NoSuchPaddingException e) {
			LOG.error("填充错误",e);
		} catch (IllegalBlockSizeException e) {
			LOG.error("非法数据块",e);
		} catch (BadPaddingException e) {
			LOG.error("错误的填充",e);
		}
		return null;
	}
	 
	/**
	 * 解密
	 * 
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return 返回解密后的原始数据
	 * @throws DesException
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] src, byte[] key) {
		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		try {
			// 从原始密匙数据创建一个DESKeySpec对象
			DESKeySpec dks = new DESKeySpec(key);
			// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
			// 一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
			SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher对象实际完成解密操作
			Cipher cipher = Cipher.getInstance(DES);
			// 用密匙初始化Cipher对象
			cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
			// 现在,获取数据并解密
			// 正式执行解密操作
			return cipher.doFinal(src);
		} catch (NoSuchAlgorithmException e) {
			LOG.error("算数错误",e);
		} catch (InvalidKeyException e) {
			LOG.error("无效key错误",e);
		} catch (InvalidKeySpecException e) {
			LOG.error("无效key戳无",e);
		} catch (NoSuchPaddingException e) {
			LOG.error("填充错误",e);
		} catch (IllegalBlockSizeException e) {
			LOG.error("非法数据块",e);
		} catch (BadPaddingException e) {
			LOG.error("错误的填充",e);
		}
		return null;
	}
}
 

你可能感兴趣的:(des)