com.rt.core.util.SecurityUtil Maven / Gradle / Ivy
package com.rt.core.util;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import com.rt.core.constant.RTConst;
class SecurityUtil {
private final static String DES = "DES";
private final static String APPEND_KEY = "b57e6f1ce627abe466522497c6712722b4ea32f2";
public static String enparam(Map param) {
if (param == null || param.isEmpty()) {
return null;
}
Map p = new HashMap();
for (Iterator iterator = param.keySet().iterator(); iterator.hasNext();) {
Object k = (Object) iterator.next();
Object v = param.get(k);
if (v == null) {
continue;
}
if (v instanceof String || v instanceof Number) {
p.put(k, v);
}
}
// 将获取到的参数放入数组
Object[] o = new Object[p.size()];
int ii = 0;
for (Iterator iterator = p.keySet().iterator(); iterator.hasNext();) {
Object k = (Object) iterator.next();
o[ii] = String.valueOf(param.get(k));
ii++;
}
// 排序
Arrays.sort(o);
StringBuffer ss = new StringBuffer();
for (int i = 0; i < o.length; i++) {
ss.append(o[i]);
}
return getSHA1Value(getMD5Value(ss.toString()));
}
/**
* 对输入字符串进行SHA1加密
*
* @param str
* 输入串
* @return 加密后的字符串
*/
public static String getSHA1Value(String str) {
StringBuffer strResult = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] encryptedBytes = md.digest();
String stmp;
for (int n = 0; n < encryptedBytes.length; n++) {
stmp = (Integer.toHexString(encryptedBytes[n] & 0XFF));
if (stmp.length() == 1) {
strResult.append("0");
strResult.append(stmp);
} else {
strResult.append(stmp);
}
}
} catch (NoSuchAlgorithmException e) {
return null;
}
return strResult.toString();
}
/**
* 对输入字符串进行SHA1加密
*
* @param str
* 输入串
* @return 加密后的字符串
*/
public static String getMD5Value(String str) {
StringBuffer strResult = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance("md5");
md.update(str.getBytes(RTConst.UTF_8));
byte[] encryptedBytes = md.digest();
String stmp;
for (int n = 0; n < encryptedBytes.length; n++) {
stmp = (Integer.toHexString(encryptedBytes[n] & 0XFF));
if (stmp.length() == 1) {
strResult.append("0");
strResult.append(stmp);
} else {
strResult.append(stmp);
}
}
} catch (Exception e) {
return null;
}
return strResult.toString();
}
/**
* 加密字节数组 key 至少8位
*
* @param key
* @param src
* @return byte[]
*/
public static byte[] encrypt(byte[] key, byte[] src) {
key = checkKey(key);
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
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 (Exception e) {
return null;
}
}
/**
* 加密字符串
*
* @param key
* @param code
* @return String
*/
public static String encryptDesByHex(String key, String code) {
return RTUtil.toHex(encrypt(key.getBytes(), code.getBytes()));
}
/**
* 解密字节数组
*
* @param key
* @param src
* @return byte[]
*/
public static byte[] decrypt(byte[] key, byte[] src) {
key = checkKey(key);
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个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 (Exception e) {
return null;
}
}
/**
* 解密字符串
*
* @param key
* @param code
* @return String
*/
public static String decryptDesByHex(String key, String code) {
return new String(decrypt(key.getBytes(), RTUtil.hexToByte(code)));
}
/**
* 获取KEY
*
* @param key
* @return Key
* @throws NoSuchAlgorithmException
*/
protected static Key getKey(byte[] key) throws NoSuchAlgorithmException {
KeyGenerator generator = KeyGenerator.getInstance(DES);
generator.init(new SecureRandom(key));
return generator.generateKey();
}
/**
* 检查key
*
* @param key
* @return byte[]
*/
protected static byte[] checkKey(byte[] key) {
if (key == null || key.length < 8) {
StringBuffer s = new StringBuffer();
if (key != null) {
s.append(new String(key));
}
s.append(APPEND_KEY);
return s.toString().getBytes();
}
System.out.println("old key:" + key);
return key;
}
}