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}