趁着週末,對前段工作做一個小總結.
首先,從Cipher的加解密開始,這裏寫了一個簡單的加解密工具
構造函數
public class EncryptUtil {
/**
* 加密算法
*/
final String ALGORITHM = "DES";
/**
* 默認密鑰
*/
final String DEFAULTKEY = "ManerFan";
/** 加密工具 */
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
/**
* 默認構造方法,使用默認密鑰及算法
*
* @throws Exception
*/
public EncryptUtil() throws Exception {
this(DEFAULTKEY, ALGORITHM);
}
/**
* 指定密鑰構造方法
*
* @param strKey 指定的密鑰
* @param algorithm 指定的算法
* @throws Exception
*/
public EncryptUtil(String strKey, String algorithm) throws Exception {
Key key = getKey(strKey.getBytes("utf-8"), algorithm);
encryptCipher = Cipher.getInstance(algorithm);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance(algorithm);
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* 從指定字符串生成密鑰,密鑰所需的字節數組長度爲8位 不足8位時後面補0,超出8位只取前8位
*
* @param byteArrTmp 構成該字符串的字節數組
* @return 生成的密鑰
* @throws java.lang.Exception
*/
private Key getKey(byte[] byteArrTmp, String algorithm) throws Exception {
// 創建一個空的8位字節數組(默認值爲0)
byte[] byteArr = new byte[8];
// 將原始字節數組轉換爲8位
for (int i = 0; i < byteArrTmp.length && i < byteArr.length; i++) {
byteArr[i] = byteArrTmp[i];
}
// 生成密鑰
Key key = new javax.crypto.spec.SecretKeySpec(byteArr, algorithm);
return key;
}
}
byte與對應十六進制字符轉換
這裏需要兩個轉換函數,實現將加密後得到的byte數組轉換爲其在內存中所表示的十六進制的字符串
/**
* 將byteArr在內存中所表示的16進制轉換爲字符串 00~FF
*
* @param byteArr 需要轉換的byte數組
* @return 轉換後的字符串
*/
public static String byteArr2HexStr(byte[] byteArr) throws Exception {
int byteLen = byteArr.length;
// 每個byte用兩個字符才能表示,所以字符串的長度是數組長度的兩倍
StringBuffer sb = new StringBuffer(byteLen * 2);
for (int i = 0; i < byteLen; i++) {
int intTmp = byteArr[i];
// 把負數轉換爲正數
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小於0F的數需要在前面補0
if (intTmp < 16) {
sb.append("0");
}
// 將byteArr在內存中所表示的16進制轉換爲字符串 00~FF
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[] byteArr = strIn.getBytes("utf-8");
int arrLen = byteArr.length;
// 兩個字符表示一個字節,所以字節數組長度是字符串長度除以2
byte[] arrOut = new byte[arrLen / 2];
for (int i = 0; i < arrLen; i = i + 2) {
// 兩位兩位取出
String strTmp = new String(byteArr, i, 2, "utf-8");
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
加密字符串
/**
* 加密字節數組
*
* @param arrB
* 需加密的字節數組
* @return 加密後的字節數組
* @throws Exception
*/
private byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密後的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
if (null == strIn || strIn.isEmpty()) {
return null;
}
return byteArr2HexStr(encrypt(strIn.getBytes("utf-8")));
}
解密字符串
/**
* 解密字節數組
*
* @param arrB
* 需解密的字節數組
* @return 解密後的字節數組
* @throws Exception
*/
private byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密後的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
if (null == strIn || strIn.isEmpty()) {
return null;
}
return new String(decrypt(hexStr2ByteArr(strIn)), "utf-8");
}
至此,加密與解密的功能就全部實現了