java字符串加密解密
try { String test = EncryptionDecryption des = new EncryptionDecryption(tourhb)// 自定義密鑰Systemoutprintln(加密前的字符 + test)Systemoutprintln(加密後的字符 + desencrypt(test))Systemoutprintln(解密後的字符 + desdecrypt(desencrypt(test)))
Systemoutprintln(解密後的字符 + desdecrypt(feccfdabdbe))
} catch (Exception e) { eprintStackTrace()}
下面是加密解密類的源碼
import javasecurityKeyimport javasecuritySecurity
import javaxcryptoCipher
public class EncryptionDecryption {
private static String strDefaultKey = tourhb
/** 加密工具 */ private Cipher encryptCipher = null
/** 解密工具 */ private Cipher decryptCipher = null
/** * 將byte數組轉換為表示進制值的字符串 如byte[]{}轉換為 和public static byte[] * hexStrByteArr(String strIn) 互為可逆的轉換過程* * @param arrB * 需要轉換的byte數組* @return 轉換後的字符串* @throws Exception * */ public static String byteArrHexStr(byte[] arrB) throws Exception { int iLen = arrBlength// 每個byte用兩個字符才能表示所以字符串的長度是數組長度的兩倍StringBuffer sb = new StringBuffer(iLen * )for (int i = i < iLen i++) { int intTmp = arrB[i]// 把負數轉換為正數while (intTmp < ) { intTmp = intTmp + } // 小於F的數需要在前面補 if (intTmp < ) { sbappend()} sbappend(IntegertoString(intTmp ))} return sbtoString()}
/** * 將表示進制值的字符串轉換為byte數組 和public static String byteArrHexStr(byte[] arrB)
* 互為可逆的轉換過程* * @param strIn 需要轉換的字符串* @return 轉換後的byte數組* @throws Exception * */ public static byte[] hexStrByteArr(String strIn) throws Exception { byte[] arrB = strIngetBytes()int iLen = arrBlength
// 兩個字符表示一個字節所以字節數組長度是字符串長度除以 byte[] arrOut = new byte[iLen / ]for (int i = i < iLen i = i + ) { String strTmp = new String(arrB i )arrOut[i / ] = (byte) IntegerparseInt(strTmp )} return arrOut}
/** * 默認構造方法使用默認密鑰* * @throws Exception */ public EncryptionDecryption() throws Exception { this(strDefaultKey)}
/** * 指定密鑰構造方法* * @param strKey * 指定的密鑰* @throws Exception */ public EncryptionDecryption(String strKey) throws Exception { SecurityaddProvider(new comsuncryptoproviderSunJCE())Key key = getKey(strKeygetBytes())
encryptCipher = CiphergetInstance(DES)encryptCipherinit(CipherENCRYPT_MODE key)
decryptCipher = CiphergetInstance(DES)decryptCipherinit(CipherDECRYPT_MODE key)}
/** * 加密字節數組* * @param arrB * 需加密的字節數組* @return 加密後的字節數組* @throws Exception */ public byte[] encrypt(byte[] arrB) throws Exception { return encryptCipherdoFinal(arrB)}
/** * 加密字符串* * @param strIn * 需加密的字符串* @return 加密後的字符串* @throws Exception */ public String encrypt(String strIn) throws Exception { return byteArrHexStr(encrypt(strIngetBytes()))}
/** * 解密字節數組* * @param arrB * 需解密的字節數組* @return 解密後的字節數組* @throws Exception */ public byte[] decrypt(byte[] arrB) throws Exception { return decryptCipherdoFinal(arrB)}
/** * 解密字符串* * @param strIn * 需解密的字符串* @return 解密後的字符串* @throws Exception */ public String decrypt(String strIn) throws Exception { try { return new String(decrypt(hexStrByteArr(strIn)))} catch (Exception e) { return }
/** * 從指定字符串生成密鑰密鑰所需的字節數組長度為位 不足位時後面補超出位只取前位* * @param arrBTmp * 構成該字符串的字節數組* @return 生成的密鑰* @throws javalangException */ private Key getKey(byte[] arrBTmp) throws Exception { // 創建一個空的位字節數組(默認值為)
byte[] arrB = new byte[]
// 將原始字節數組轉換為位for (int i = i < arrBTmplength && i < arrBlength i++) { arrB[i] = arrBTmp[i]}
// 生成密鑰Key key = new javaxcryptospecSecretKeySpec(arrB DES)
return key}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26933.html