package com.bmcc.framework.util;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密算法工具类,MD5和DES
*
* @author yuhu.zhang
*/
public class CMyCipher {
/**
* AES加密KEY
*/
public static final String AES_ENCRYPT_KEY = "aes-key--key-aes";
/**
* md5加密
*
* @param target
* 待加密字符串
* @return 加密后的MD5字符串
* @throws Exception
* 异常
*/
public static String encryptMd5(String target) throws Exception {
if (target == null || target.length() <= 0) {
return null;
}
MessageDigest md5 = MessageDigest.getInstance("MD5");
return byte2hex(md5.digest(target.getBytes("utf-8")));
}
/**
* 利用java原生的摘要实现SHA256加密
*
* @param target 需要加密的字符串
* @return String
* @throws Exception 异常
*/
public static String encryptSha256(String target) throws Exception {
if (target == null || target.length() == 0) {
return null;
}
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(target.getBytes("UTF-8"));
return byte2hex(messageDigest.digest());
}
/**
* 加密密码,先MD5加密
*
* @param target
* 密码字符串
* @return 加密后的密码
* @throws Exception
* 异常
*/
public static String encryptPassword(String target) throws Exception {
return encryptMd5(target);
}
/**
* 加密
*
* @param target
* 加密对象
* @return 返回加密后的字符串
* @throws Exception
* 异常
*/
public static String encryptAes(String target) throws Exception {
return CMyCipher.encryptAes(target, AES_ENCRYPT_KEY);
}
/**
* 加密
*
* @param target
* 加密对象
* @param sKey
* 加密密钥
* @return 加密后的字符串
* @throws Exception
* 异常
*/
public static String encryptAes(String target, String sKey) throws Exception {
if (target == null || target.length() <= 0) {
return null;
}
// 判断Key是否正确
if (CMyString.isEmpty(sKey)) {
sKey = AES_ENCRYPT_KEY;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
throw new IllegalArgumentException("illegal sKey");
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(target.getBytes());
return byte2hex(encrypted);
}
/**
* 解密
*
* @param target
* 解密对象
* @return 解密后的原数据
* @throws Exception
* 异常
*/
public static String decryptAes(String target) throws Exception {
return decryptAes(target, AES_ENCRYPT_KEY);
}
/**
* 解密
*
* @param target
* 解密对象
* @param sKey
* 加密密钥
* @return 解密后的原数据
* @throws Exception
* 异常
*
*/
public static String decryptAes(String target, String sKey) throws Exception {
if (target == null || target.length() <= 0) {
return null;
}
if (CMyString.isEmpty(sKey)) {
sKey = AES_ENCRYPT_KEY;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
throw new IllegalArgumentException("illegal sKey");
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = hex2byte(target.getBytes());
byte[] original = cipher.doFinal(encrypted1);
return new String(original, "UTF-8");
}
// 二行制转字符串
private static String byte2hex(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
hs.append('0');
}
hs.append(stmp);
}
return hs.toString().toLowerCase();
}
// 二行制转字符数组
private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0) {
throw new IllegalArgumentException();
}
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;
}
/**
* @param args
* 参数
* @throws Exception
* 异常
*/
public static void main(String[] args) throws Exception {
System.out.println(encryptMd5("18810000000"));
System.out.println(encryptSha256("18810000000"));
}
}