以下兩個類可以很方便的完成字符串的加密和解密
加密
解密
代碼如下
CryptUtils
[java]
package com
import java
import javax
import javax
import javax
import
public class CryptUtils {
private static String Algorithm =
private static byte[] DEFAULT_KEY=new byte[] {
private static String VALUE_ENCODING=
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator
SecretKey deskey = keygen
// if (debug ) System
// ()))
return deskey
}
/**
* 將指定的數據根據提供的密鑰進行加密
*
* @param input
* 需要加密的數據
* @param key
* 密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte[] encryptData(byte[] input
SecretKey deskey = new javax
// if (debug )
// {
// System
// System
//
// }
Cipher c
c
byte[] cipherByte = c
// if (debug ) System
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密後的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input
SecretKey deskey = new javax
// if (debug ) System
Cipher c
c
byte[] clearByte = c
// if (debug )
// {
// System
// System
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input
}
/**
* 字節碼轉換成
*
* @param byte[] b 輸入要轉換的字節碼
* @return String 返回轉換後的
*/
public static String byte
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs
return hs
}
public static byte[] hex
int l=content
byte[] result=new byte[l];
for(int i=
int j=i《
String s=content
result[i]=Integer
}
return result;
}
/**
* 將字節數組轉換為base
* @param buffer
* @return
*/
public static String bytesToBase
//BASE
return Base
// return encoder
}
/**
* 將base
* @param value
* @return
* @throws IOException
*/
public static byte[] base
//return Base
// System
// return decoder
return Base
}
/**
* 加密給定的字符串
* @param value
* @return 加密後的base
*/
public static String encryptString(String value) {
return encryptString(value
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 以BASE
* @return 加密後的base
* @throws IOException
*/
public static String encryptString(String value
return encryptString(value
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 字節數組形式的密鑰
* @return 加密後的base
*/
public static String encryptString(String value
try {
byte[] data=value
data=CryptUtils
return bytesToBase
} catch (Exception e) {
// TODO Auto
e
return null;
}
}
/**
* 解密字符串
* @param value base
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value
}
/**
* 解密字符串
* @param value base
* @param key base
* @return 明文
* @throws IOException
*/
public static String decryptString(String value
String s=decryptString(value
return s;
}
/**
* 解密字符串
* @param value base
* @param key 字節數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value
try {
byte[] data=base
data=CryptUtils
return new String(data
}catch(Exception e) {
e
return null;
}
}
}
package com
import java
import javax
import javax
import javax
import
public class CryptUtils {
private static String Algorithm =
private static byte[] DEFAULT_KEY=new byte[] {
private static String VALUE_ENCODING=
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator
SecretKey deskey = keygen
// if (debug ) System
// ()))
return deskey
}
/**
* 將指定的數據根據提供的密鑰進行加密
*
* @param input
* 需要加密的數據
* @param key
* 密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte[] encryptData(byte[] input
SecretKey deskey = new javax
// if (debug )
// {
// System
// System
//
// }
Cipher c
c
byte[] cipherByte = c
// if (debug ) System
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密後的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input
SecretKey deskey = new javax
// if (debug ) System
Cipher c
c
byte[] clearByte = c
// if (debug )
// {
// System
// System
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input
}
/**
* 字節碼轉換成
*
* @param byte[] b 輸入要轉換的字節碼
* @return String 返回轉換後的
*/
public static String byte
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs
return hs
}
public static byte[] hex
int l=content
byte[] result=new byte[l];
for(int i=
int j=i《
String s=content
result[i]=Integer
}
return result;
}
/**
* 將字節數組轉換為base
* @param buffer
* @return
*/
public static String bytesToBase
//BASE
return Base
// return encoder
}
/**
* 將base
* @param value
* @return
* @throws IOException
*/
public static byte[] base
//return Base
// System
// return decoder
return Base
}
/**
* 加密給定的字符串
* @param value
* @return 加密後的base
*/
public static String encryptString(String value) {
return encryptString(value
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 以BASE
* @return 加密後的base
* @throws IOException
*/
public static String encryptString(String value
return encryptString(value
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 字節數組形式的密鑰
* @return 加密後的base
*/
public static String encryptString(String value
try {
byte[] data=value
data=CryptUtils
return bytesToBase
} catch (Exception e) {
// TODO Auto
e
return null;
}
}
/**
* 解密字符串
* @param value base
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value
}
/**
* 解密字符串
* @param value base
* @param key base
* @return 明文
* @throws IOException
*/
public static String decryptString(String value
String s=decryptString(value
return s;
}
/**
* 解密字符串
* @param value base
* @param key 字節數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value
try {
byte[] data=base
data=CryptUtils
return new String(data
}catch(Exception e) {
e
return null;
}
}
}
CryptHelper
[java]
package com
import javax
import javax
import javax
import javax
import javax
import org
public class CryptHelper{
private static String CRYPT_KEY =
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils
KEY = KEY
byte[] bytes = KEY
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory
SecretKey sk = skf
IvParameterSpec iv
ecip = Cipher
ecip
dcip = Cipher
dcip
}catch(Exception ex) {
ex
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip
return CryptUtils
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils
bytes = dcip
return new String(bytes
}
//test
public static void main(String[] args) throws Exception {
String password =
String en = encrypt(password)
System
System
}
}
package com
import javax
import javax
import javax
import javax
import javax
import org
public class CryptHelper{
private static String CRYPT_KEY =
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils
KEY = KEY
byte[] bytes = KEY
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory
SecretKey sk = skf
IvParameterSpec iv
ecip = Cipher
ecip
dcip = Cipher
dcip
}catch(Exception ex) {
ex
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip
return CryptUtils
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils
bytes = dcip
return new String(bytes
}
//test
public static void main(String[] args) throws Exception {
String password =
String en = encrypt(password)
System
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26449.html