第一种方法:
import java.security.Key;import javax.crypto.Cipher;
public class Test {
/**
* 默认构造方法,使用默认密钥
*/
public Test() throws Exception {
this(strDefaultKey);
}
/**
* 指定密钥构造方法
* @param strKey 指定的密钥
* @throws Exception
*/
public Test(String strKey) throws Exception {
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/** 字符串默认键值 */
private static String strDefaultKey = "abcdefg123456";
/** 加密工具 */
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
* @param strIn 需要转换的字符串
* @return 转换后的byte数组
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
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);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 加密字节数组
* @param arrB 需加密的字节数组
* @return 加密后的字节数组
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
* @param strIn 需加密的字符串
* @return 加密后的字符串
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
* 解密字节数组
* @param arrB 需解密的字节数组
* @return 解密后的字节数组
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
* @param strIn 需解密的字符串
* @return 解密后的字符串
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) {
try {
String test1 = "987654321";
Test des1 = new Test();// 使用默认密钥
System.out.println("加密前的字符:" + test1);
System.out.println("加密后的字符:" + des1.encrypt(test1));
System.out.println("解密后的字符:" + des1.decrypt(des1.encrypt(test1)));
System.out.println("*********************************************");
String test2 = "123456789";
Test des2 = new Test("leeme32nz");// 自定义密钥
System.out.println("加密前的字符:" + test2);
System.out.println("加密后的字符:" + des2.encrypt(test2));
System.out.println("解密后的字符:" + des2.decrypt(des2.encrypt(test2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
第二种方法:
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==";
/**
* <li>
* 方法名称:encrypt</li> <li>
* 加密方法
*
* @param xmlStr
* 需要加密的消息字符串
* @return 加密后的字符串
*/
public static String encrypt(String xmlStr) {
byte[] encrypt = null;
try {
// 取需要加密内容的utf-8编码。
encrypt = xmlStr.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 取MD5Hash码,并组合加密数组
byte[] md5Hasn = null;
try {
md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);
} catch (Exception e) {
e.printStackTrace();
}
// 组合消息体
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);
// 使用DES算法使用加密消息体
byte[] temp = null;
try {
temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey, ivParam);
} catch (Exception e) {
e.printStackTrace();
}
// 使用Base64加密后返回
return new BASE64Encoder().encode(temp);
}
/**
* <li>
* 方法名称:encrypt</li> <li>
* 功能描述:
*
* <pre>
* 解密方法
* </pre>
*
* </li>
*
* @param xmlStr
* 需要解密的消息字符串
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String xmlStr) throws Exception {
// base64解码
BASE64Decoder decoder = new BASE64Decoder();
byte[] encBuf = null;
try {
encBuf = decoder.decodeBuffer(xmlStr);
} catch (IOException e) {
e.printStackTrace();
}
// 取密钥和偏转向量
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);
// 使用DES算法解密
byte[] temp = null;
try {
temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);
} catch (Exception e) {
e.printStackTrace();
}
// 进行解密后的md5Hash校验
byte[] md5Hash = null;
try {
md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);
} catch (Exception e) {
e.printStackTrace();
}
// 进行解密校检
for (int i = 0; i < md5Hash.length; i++) {
if (md5Hash[i] != temp[i]) {
// System.out.println(md5Hash[i] + "MD5校验错误。" + temp[i]);
throw new Exception("MD5校验错误。");
}
}
// 返回解密后的数组,其中前16位MD5Hash码要除去。
return new String(temp, 16, temp.length - 16, "utf-8");
}
/**
* <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;
StringBuffer out = new StringBuffer(inStr.length * 2);
for (int n = 0; n < inStr.length; n++) {
// 字节做"与"运算,去除高位置字节 11111111
stmp = Integer.toHexString(inStr[n] & 0xFF);
if (stmp.length() == 1) {
// 如果是0至F的单位字符串,则添加0
out.append("0" + stmp);
} else {
out.append(stmp);
}
}
return out.toString();
}
/**
* <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];
}
}
public static void main(String[] args) {
try {
System.out.println(encrypt("123456"));
System.out.println(decrypt("oqGiG3w2C/s4l945xI++My4Wpv2cCyLi"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}