private static final String Default_Key = "A3F2DEI569WBCJSJEOTY45DYQWF68H1Y";
public static String encryptString(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
return encryptString(Default_Key, value);
}
public static String encryptString(String key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
byte[] bytesKey = null, bytes = null, bytesCipher = null;
SecretKey deskey = null;
if (value == null)
new IllegalArgumentException("待加密字串不允许为空");
//密码器
Cipher desCipher = Cipher.getInstance(Algorithm);
try{
bytesKey = Base64.decodeBase64(key);
deskey = new SecretKeySpec(bytesKey, Algorithm);
bytes = value.getBytes();//待解密的字串
desCipher.init(Cipher.ENCRYPT_MODE, deskey);//初始化密码器,用密钥deskey,进入解密模式
bytesCipher = desCipher.doFinal(bytes);
return Base64.encodeBase64String(bytesCipher).trim();
}
finally{
bytesKey = null;
bytes = null;
bytesCipher = null;
}
}
public static String decryptString(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
return decryptString(Default_Key, value);
}
public static String decryptString(String key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
byte[] bytesKey = null, bytes = null, bytesCipher = null;
SecretKey deskey = null;
if (value == null)
new IllegalArgumentException("待解密字串不允许为空");
//密码器
Cipher desCipher = Cipher.getInstance(Algorithm);
try{
bytesKey = Base64.decodeBase64(key);
deskey = new SecretKeySpec(bytesKey, Algorithm);
bytes = Base64.decodeBase64(value);//加密后的字串
desCipher.init(Cipher.DECRYPT_MODE, deskey);//初始化密码器,用密钥deskey,进入解密模式
bytesCipher = desCipher.doFinal(bytes);
return (new String(bytesCipher,"UTF-8"));
}
finally{
bytesKey = null;
bytes = null;
bytesCipher = null;
}
}
public static void main(String args[]){
try{
String strEncode = encryptString("我是谁");
System.out.println("加密返回:"+strEncode);
String strOrg = decryptString(strEncode);
System.out.println("解密返回:"+strOrg);
}
catch(Exception e){
e.printStackTrace();
}
}
/// <summary>
/// 默认Key
/// </summary>
private const string Default_Key = "A3F2DEI569WBCJSJEOTY45DYQWF68H1Y";
/// <summary>
/// 默认IV 矢量,矢量可以为空
/// </summary>
private const string Default_IV = "qcDX+Y6aPLw=";
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string EncryptString(string value)
{
return EncryptString(Default_Key, Default_IV, value);
}
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="key">Key键</param>
/// <param name="iv">向量</param>
/// <param name="value">要加密的串</param>
/// <returns>加密后的字串</returns>
public static string EncryptString(string key, string iv, string value)
{
using (SymmetricAlgorithm desCSP = new TripleDESCryptoServiceProvider())
{
desCSP.Key = Convert.FromBase64String(key);
desCSP.IV = Convert.FromBase64String(iv); //指定加密的运算模式
desCSP.Mode = System.Security.Cryptography.CipherMode.ECB; //获取或设置加密算法的填充模式
desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ctTrans = desCSP.CreateEncryptor(desCSP.Key, desCSP.IV);
byte[] bytes = Encoding.UTF8.GetBytes(value);
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(memStream, ctTrans, CryptoStreamMode.Write))
{
csEncrypt.Write(bytes, 0, bytes.Length);
csEncrypt.FlushFinalBlock();
csEncrypt.Close();
}
bytes = null; //清理内存
return Convert.ToBase64String(memStream.ToArray());
}
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string DecryptString(string value)
{
return DecryptString(Default_Key, Default_IV, value);
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="key">Key键</param>
/// <param name="iv">向量</param>
/// <param name="value">要解密的串</param>
/// <returns>解密后的字串</returns>
public static string DecryptString(string key, string iv, string value)
{
using (SymmetricAlgorithm desCSP = new TripleDESCryptoServiceProvider())
{
desCSP.Key = Convert.FromBase64String(key);
desCSP.IV = Convert.FromBase64String(iv); //指定加密的运算模式
desCSP.Mode = System.Security.Cryptography.CipherMode.ECB; //获取或设置加密算法的填充模式
desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ctTrans = desCSP.CreateDecryptor(desCSP.Key, desCSP.IV);
byte[] bytes = Convert.FromBase64String(value);
byte[] bytesOut = new byte[10240];
int iReadLen = 0;
using (MemoryStream memStream = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(memStream, ctTrans, CryptoStreamMode.Read))
{
using (MemoryStream outStream = new MemoryStream())
{
while ((iReadLen = csDecrypt.Read(bytesOut, 0, bytesOut.Length)) > 0)
{
outStream.Write(bytesOut, 0, iReadLen);
}
bytes = null;
bytesOut = null;//清理内存
return System.Text.Encoding.UTF8.GetString(outStream.ToArray());
}
}
}
}
}
#endregion