算法可逆,具有跨平台特性
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 功能描述 加密常用類 特性跨平台
*/
public class EncryptUtil {
// 密鑰是16位長度的byte[]進行Base64轉換后得到的字符串
// public static String key = "LmMGStGtOpF4xNyvYt54EQ==";
public static String key = "cc839cf9feba4ed7ba68064177a0b505";
/**
* <li>方法名稱:encrypt</li>
* <li>加密方法
*
* @param xmlStr
* 需要加密的消息字符串
* @return 加密后的字符串
*/
public static String encrypt(String xmlStr) {
// 使用DES算法使用加密消息體
String result = null;
try {
// 取需要加密內容的utf-8編碼。
byte[] encrypt = xmlStr.getBytes("utf-8");
// 取MD5Hash碼,並組合加密數組
byte[] md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);
// 組合消息體
byte[] totalByte = EncryptUtil.addMD5(md5Hasn, encrypt);
// 取密鑰和偏轉向量
byte[] key = new byte[8];
byte[] iv = new byte[8];
getKeyIV(EncryptUtil.key, key, iv);
SecretKeySpec deskey = new SecretKeySpec(key, "DES");
IvParameterSpec ivParam = new IvParameterSpec(iv);
byte[] temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey, ivParam);
result = new BASE64Encoder().encode(temp);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// 使用Base64加密后返回
return result;
}
/**
* <li>方法名稱:encrypt</li>
* <li>功能描述:
*
* <pre>
* 解密方法
* </pre>
*
* </li>
*
* @param xmlStr
* 需要解密的消息字符串
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String xmlStr) {
// 使用DES算法解密
String result = null;
try {
// base64解碼
BASE64Decoder decoder = new BASE64Decoder();
byte[] encBuf = decoder.decodeBuffer(xmlStr);
// 取密鑰和偏轉向量
byte[] key = new byte[8];
byte[] iv = new byte[8];
getKeyIV(EncryptUtil.key, key, iv);
SecretKeySpec deskey = new SecretKeySpec(key, "DES");
IvParameterSpec ivParam = new IvParameterSpec(iv);
byte[] temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);
// 進行解密后的md5Hash校驗
byte[] md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);
// 進行解密校檢
for (int i = 0; i < md5Hash.length; i++) {
if (md5Hash[i] != temp[i]) {
throw new Exception();
}
}
result = new String(temp, 16, temp.length - 16, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
// 返回解密后的數組,其中前16位MD5Hash碼要除去。
return result;
}
/**
* <li>方法名稱:TripleDES_CBC_Encrypt</li>
* <li>功能描述:
*
* <pre>
* 經過封裝的三重DES/CBC加密算法,如果包含中文,請注意編碼。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要加密內容的字節數組。
* @param deskey
* KEY 由24位字節數組通過SecretKeySpec類轉換而成。
* @param ivParam
* IV偏轉向量,由8位字節數組通過IvParameterSpec類轉換而成。
* @return 加密后的字節數組
* @throws Exception
*/
public static byte[] TripleDES_CBC_Encrypt(byte[] sourceBuf, SecretKeySpec deskey, IvParameterSpec ivParam)
throws Exception {
byte[] cipherByte;
// 使用DES對稱加密算法的CBC模式加密
Cipher encrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");
encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);
cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回加密后的字節數組
return cipherByte;
}
/**
* <li>方法名稱:TripleDES_CBC_Decrypt</li>
* <li>功能描述:
*
* <pre>
* 經過封裝的三重DES / CBC解密算法
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要解密內容的字節數組
* @param deskey
* KEY 由24位字節數組通過SecretKeySpec類轉換而成。
* @param ivParam
* IV偏轉向量,由6位字節數組通過IvParameterSpec類轉換而成。
* @return 解密后的字節數組
* @throws Exception
*/
public static byte[] TripleDES_CBC_Decrypt(byte[] sourceBuf, SecretKeySpec deskey, IvParameterSpec ivParam)
throws Exception {
byte[] cipherByte;
// 獲得Cipher實例,使用CBC模式。
Cipher decrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");
// 初始化加密實例,定義為解密功能,並傳入密鑰,偏轉向量
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);
cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回解密后的字節數組
return cipherByte;
}
/**
* <li>方法名稱:DES_CBC_Encrypt</li>
* <li>功能描述:
*
* <pre>
* 經過封裝的DES/CBC加密算法,如果包含中文,請注意編碼。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要加密內容的字節數組。
* @param deskey
* KEY 由8位字節數組通過SecretKeySpec類轉換而成。
* @param ivParam
* IV偏轉向量,由8位字節數組通過IvParameterSpec類轉換而成。
* @return 加密后的字節數組
* @throws Exception
*/
public static byte[] DES_CBC_Encrypt(byte[] sourceBuf, SecretKeySpec deskey, IvParameterSpec ivParam)
throws Exception {
byte[] cipherByte;
// 使用DES對稱加密算法的CBC模式加密
Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);
cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回加密后的字節數組
return cipherByte;
}
/**
* <li>方法名稱:DES_CBC_Decrypt</li>
* <li>功能描述:
*
* <pre>
* 經過封裝的DES/CBC解密算法。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要解密內容的字節數組
* @param deskey
* KEY 由8位字節數組通過SecretKeySpec類轉換而成。
* @param ivParam
* IV偏轉向量,由6位字節數組通過IvParameterSpec類轉換而成。
* @return 解密后的字節數組
* @throws Exception
*/
public static byte[] DES_CBC_Decrypt(byte[] sourceBuf, SecretKeySpec deskey, IvParameterSpec ivParam)
throws Exception {
byte[] cipherByte;
// 獲得Cipher實例,使用CBC模式。
Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 初始化加密實例,定義為解密功能,並傳入密鑰,偏轉向量
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);
cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回解密后的字節數組
return cipherByte;
}
/**
* <li>方法名稱:MD5Hash</li>
* <li>功能描述:
*
* <pre>
* MD5,進行了簡單的封裝,以適用於加,解密字符串的校驗。
* </pre>
*
* </li>
*
* @param buf
* 需要MD5加密字節數組。
* @param offset
* 加密數據起始位置。
* @param length
* 需要加密的數組長度。
* @return
* @throws Exception
*/
public static byte[] MD5Hash(byte[] buf, int offset, int length) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buf, offset, length);
return md.digest();
}
/**
* <li>方法名稱:byte2hex</li>
* <li>功能描述:
*
* <pre>
* 字節數組轉換為二行制表示
* </pre>
*
* </li>
*
* @param inStr
* 需要轉換字節數組。
* @return 字節數組的二進制表示。
*/
public static String byte2hex(byte[] inStr) {
String stmp;
// out = new StringBuffer(inStr.length * 2);
String out = new String("");
for (int n = 0; n < inStr.length; n++) {
// 字節做"與"運算,去除高位置字節 11111111
stmp = Integer.toHexString(inStr[n] & 0xFF);
if (stmp.length() == 1) {
// 如果是0至F的單位字符串,則添加0
out = out + "0" + stmp;
} else {
out = out + stmp;
}
}
return out;
}
/**
* <li>方法名稱:addMD5</li>
* <li>功能描述:
*
* <pre>
* MD校驗碼 組合方法,前16位放MD5Hash碼。 把MD5驗證碼byte[],加密內容byte[]組合的方法。
* </pre>
*
* </li>
*
* @param md5Byte
* 加密內容的MD5Hash字節數組。
* @param bodyByte
* 加密內容字節數組
* @return 組合后的字節數組,比加密內容長16個字節。
*/
public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) {
int length = bodyByte.length + md5Byte.length;
byte[] resutlByte = new byte[length];
// 前16位放MD5Hash碼
for (int i = 0; i < length; i++) {
if (i < md5Byte.length) {
resutlByte[i] = md5Byte[i];
} else {
resutlByte[i] = bodyByte[i - md5Byte.length];
}
}
return resutlByte;
}
/**
* <li>方法名稱:getKeyIV</li>
* <li>功能描述:
*
* <pre>
*
* </pre>
*
* </li>
*
* @param encryptKey
* @param key
* @param iv
*/
public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) {
// 密鑰Base64解密
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = null;
try {
buf = decoder.decodeBuffer(encryptKey);
} catch (IOException e) {
e.printStackTrace();
}
// 前8位為key
int i;
for (i = 0; i < key.length; i++) {
key[i] = buf[i];
}
// 后8位為iv向量
for (i = 0; i < iv.length; i++) {
iv[i] = buf[i + 8];
}
}
}