`
decao
  • 浏览: 77810 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

DES加密解密类

阅读更多
package com.dc;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/* ******************  类说明  *********************
 * class       :  DcDESUtil
 * @author     :  ncc
 * create time :  2017-12-8 下午05:46:02
 * @version    :  1.0  
 * description :  DES加密解密类
 * DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的
 * 对称密码体制加密算法。 明文按64位进行分组, 密钥长64位,密钥事实上是56位参与DES运算
 * (第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后的明文组
 * 和56位的密钥按位替代或交换的方法形成密文组的加密方法
 * @see        :                        
 * ************************************************/   
public class DcDESUtil {
	
	private static final int ENCRYPT_MODE = 1; // 加密模式
	private static final int DECRYPT_MODE = 2; // 解密模式
	
	// 密钥
	private static byte[] DEFKEY = "wPSnMdH3".getBytes();
	
	/* ********************************************
	 * method name   : enc_des 
	 * description   : DES加密方法
	 * @return       : String 加密后的密文
	 * @param        : @param showInfo 需要加密的明文信息
	 * @param        : @return
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	public static String enc_des(String showInfo){
		byte[] result = null;
		try {
			byte[] tmp = showInfo.getBytes("GBK");
			result = cipher(tmp, DEFKEY, ENCRYPT_MODE, "DES");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null == result ? null : b2hex(result);
	}
	
	/* ********************************************
	 * method name   : dec_des 
	 * description   : DES解密方法
	 * @return       : String 解密后的明文
	 * @param        : @param encryInfo 要解密的密文
	 * @param        : @return
	 * @param        : @throws IllegalBlockSizeException
	 * @param        : @throws IllegalArgumentException
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	public static String dec_des(String encryInfo) throws IllegalBlockSizeException,IllegalArgumentException {
		byte[] result = null;
		String resultStr = null;
		try {
			try {
				byte[] tmp = hex2b(encryInfo);
				result = cipher(tmp,DEFKEY, DECRYPT_MODE, "DES");
				resultStr = new String(result,"GBK");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return resultStr;
		} catch (ArrayIndexOutOfBoundsException e) {//base64抛出数组越界 
			throw new IllegalBlockSizeException(e.getMessage());
		} catch (NullPointerException e) {
			throw new IllegalBlockSizeException(e.getMessage());
		}
	}
	
	/* ********************************************
	 * method name   : dec_des 
	 * description   : des解密方法
	 * @return       : String
	 * @param        : @param encryInfo 密文
	 * @param        : @param key 密钥
	 * @param        : @return
	 * @param        : @throws IllegalBlockSizeException
	 * @param        : @throws IllegalArgumentException
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	public static String dec_des(String encryInfo, byte[] key) throws IllegalBlockSizeException,IllegalArgumentException {
		byte[] result = null;
		String resultStr = null;
		try {
			try {
				byte[] tmp = hex2b(encryInfo);
				result = cipher(tmp,key, DECRYPT_MODE, "DES");
				resultStr = new String(result,"GBK");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return resultStr;
		} catch (ArrayIndexOutOfBoundsException e) {//base64抛出数组越界 
			throw new IllegalBlockSizeException(e.getMessage());
		} catch (NullPointerException e) {
			throw new IllegalBlockSizeException(e.getMessage());
		}
	}
	
	/* ********************************************
	 * method name   : cipher 
	 * description   : 加密解密工具方法
	 * @return       : byte[]
	 * @param        : @param binfo 原字符数组
	 * @param        : @param bkey 密钥
	 * @param        : @param mode 模式(加密/解密)
	 * @param        : @param transformation 加密解密方式
	 * @param        : @return
	 * @param        : @throws IllegalBlockSizeException
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	private static byte[] cipher(byte[] binfo, byte[] bkey, int mode, String transformation) throws IllegalBlockSizeException {
		String algs[] = transformation.split("/");
		SecretKey key = new SecretKeySpec(bkey, algs[0]);
		Cipher cipher = null;
		byte[] result = null;
		try {
			cipher = Cipher.getInstance(transformation);
			cipher.init(mode, key);
			result = cipher.doFinal(binfo);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/* ********************************************
	 * method name   : b2hex 
	 * description   : 二进制转换成十六进制
	 * @return       : String
	 * @param        : @param bs
	 * @param        : @return
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	private  static String b2hex(byte[] bs) {
		int iLen = bs.length;
		// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = bs[i];
			// 把负数转换为正数
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			// 小于0F的数需要在前面补0
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	/* ********************************************
	 * method name   : hex2b 
	 * description   : 十六进制转换二进制
	 * @return       : byte[]
	 * @param        : @param str
	 * @param        : @return
	 * @param        : @throws UnsupportedEncodingException
	 * modified      : ncc ,  2017-12-8
	 * @see          : 
	 * ********************************************/      
	private static byte[] hex2b(String str) throws UnsupportedEncodingException {
		byte[] arrB = str.getBytes("GBK");
		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,"GBK");
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}
	
	public static void main(String[] args) {
		String str = "欢迎光临得草之家";
		String strEnc = DcDESUtil.enc_des(str);
		System.out.println("str==" + str);
		System.out.println("strEnc==" + strEnc);
		try {
			String strdec = DcDESUtil.dec_des(strEnc);
			System.out.println("strdec==" + strdec);
		} catch (IllegalBlockSizeException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			System.out.println("******************************");
			System.out.println(DcDESUtil.dec_des("747afb83ae979a5ef5d0f5742b4a72184774773ec4fc58b3", "lSbEGkW7".getBytes()));
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			System.out.println(e);
		}
		
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics