DESUtil.java 4.1 KB
package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import java.util.Random;

/** 
 * 采用MD5加密解密 
 * @author tfq 
 * @datetime 2011-10-13 
 */  
public class DESUtil {  
  
	private static final String KEY = "kimwaynet";// 密钥
	private final static String DES = "DES";// DES算法名称
	
	//加密
	private static byte[] encrypt(byte[] src, byte[] key) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance(DES);
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
		return cipher.doFinal(src);
	}
	
	//解密
	private static byte[] decrypt(byte[] src, byte[] key) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance(DES);
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
		return cipher.doFinal(src);
	}	
	
	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();

	}
	private static byte[] hex2byte(byte[] b) {
		if ((b.length % 2) != 0)
			throw new IllegalArgumentException("length not even");
		byte[] b2 = new byte[b.length / 2];
		for (int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			b2[n / 2] = (byte) Integer.parseInt(item, 16);
		}
		return b2;
	}
	
	//解密
	public static String decode(String src,String key) {
		if(StringUtils.isEmpty(key))
		{
			key = KEY;
		}
		String decryptStr = "";
		try {
			decryptStr = new String(decrypt(hex2byte(src.getBytes()),key.getBytes()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return decryptStr;
	}
	
	//加密
	public static String encode(String src,String key){
		if(StringUtils.isEmpty(key))
		{
			key = KEY;
		}
		byte[] bytes = null;
		String encryptStr = "";
		try {
			bytes = encrypt(src.getBytes(), key.getBytes());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (bytes != null)
			encryptStr = byte2hex(bytes);
		return encryptStr;
	}
	
	public static String getDecodeMAC(String decryptStr){
		String mac = "";
		String[] strs = decryptStr.split(":");
		if (strs.length == 5) {
			mac = strs[0];
		}
		return mac;
	}
  
	/**
	 * 生成指定长度的随机字符串
	 * @param args
	 */
	/**
	   * 产生随机字符串
	   * */
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;

	public static final String randomString(int length) {
	         if (length < 1) {
	             return null;
	         }
	         if (randGen == null) {
	                randGen = new Random();
	                numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
	                   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                  //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                 }
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
	          //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
	         }
	         return new String(randBuffer);
	}
	
    // 测试主函数  
    public static void main(String args[]) {  
//        String s = new String("F:/yu2le-service-log");
//        System.out.println("原始:" + s);
//        s = encode(s,"LINUXYU2LE");
//        System.out.println("加密的:" + s);
        System.out.println("解密的:" + decode("498CFDA0AA8A2E9A","kimwaynet"));

//		System.out.println(encode("13912927204","LS6GzB"));
    }  
}