欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DES算法加密,解密工具类

程序员文章站 2024-03-14 10:48:40
...
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("无效的**文件");

		}
	}
}