using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security; namespace Common.Utility
{
/// <summary>
/// Author:Kt
/// Date Created:2011-04-01
/// Description:加密对象-工具类
/// </summary>
public class EncryptHelper
{
/// <summary>
/// AES 解密
/// </summary>
/// <param name="content">内容</param>
/// <param name="secretKey">私钥(长度: 16、24、32字节)</param>
/// <returns></returns>
public static string AESDecrypt(string content, string secretKey)
{
if (string.IsNullOrEmpty(content)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(content); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(secretKey),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
}; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray);
} /// <summary>
/// AES 加密
/// </summary>
/// <param name="content">内容</param>
/// <param name="secretKey">私钥(长度: 16、24、32字节)</param>
/// <returns></returns>
public static string AESEncrypt(string content, string secretKey)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(secretKey);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(content); RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Convert.ToBase64String(resultArray, , resultArray.Length);
} /// <summary>
/// MD5 加密
/// </summary>
/// <param name="content">内容</param>
/// <returns></returns>
public static string Md5(string content)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(content, "MD5");
} /// <summary>
/// MD5 加密
/// </summary>
/// <param name="content">内容</param>
/// <param name="encode">编码</param>
/// <returns>签名字符串</returns>
public static string Md5(string content, Encoding encode)
{
var result = new StringBuilder();
var md5 = new MD5CryptoServiceProvider();
var bytes = md5.ComputeHash(encode.GetBytes(content));
foreach (var item in bytes)
result.Append(item.ToString("x").PadLeft(, '')); return result.ToString();
} /// <summary>
/// 3DES 解密
/// </summary>
/// <param name="content">内容</param>
/// <param name="secretKey">私钥</param>
/// <param name="ivs">向量</param>
/// <returns></returns>
public static string DESDecrypt(string content, string secretKey, string ivs)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
mCSP.Mode = CipherMode.CBC;
mCSP.Padding = PaddingMode.PKCS7;
mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
mCSP.IV = Encoding.UTF8.GetBytes(ivs);
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(content);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, , byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
} /// <summary>
/// 3DES 加密
/// </summary>
/// <param name="content">内容</param>
/// <param name="secretKey">私钥</param>
/// <param name="ivs">向量</param>
/// <returns></returns>
public static string DESEncrypt(string content, string secretKey, string ivs)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
mCSP.Mode = CipherMode.CBC;
mCSP.Padding = PaddingMode.PKCS7;
mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
mCSP.IV = Encoding.UTF8.GetBytes(ivs);
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(content);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, , byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
}
}
using System;
using System.Text; namespace Common.Utility.Tests
{
/// <summary>
/// 运行示例
/// </summary>
internal partial class Program
{
private static void Main_EncryptHelper(string[] args)
{
string str = "需要加密的字符串12345678";
string md5_1 = EncryptHelper.Md5(str);
string md5_2 = EncryptHelper.Md5(str, Encoding.UTF8); str = "";
string secretKey_AES = Guid.NewGuid().ToString("N");
string JiaMi = EncryptHelper.AESEncrypt(str, secretKey_AES);
string JieMi = EncryptHelper.AESDecrypt(JiaMi, secretKey_AES); string secretKey_DES = "fsT7ObM1nEnrRAGO1djI2YBi";
string ivs_DES = "GUGlYE1g";
JiaMi = EncryptHelper.DESEncrypt(str, secretKey_DES, ivs_DES);
JieMi = EncryptHelper.DESDecrypt(JiaMi, secretKey_DES, ivs_DES);
}
}
}
05-11 18:01