using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace System.CRM.Common { /// <summary> /// 加解密 助手类 /// </summary> public class EncryptionHelper { #region AES 加密 解密 /// <summary> /// AES加密 /// </summary> /// <param name="str">要加密字符串</param> /// <param name="aeskey">要加密字符串的密钥(16位或32位)</param> /// <returns>返回加密后字符串</returns> public static String EncryptAES(String str, string aeskey) { Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey); Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param name="str">要解密字符串</param> /// <param name="aeskey">要加密字符串的密钥(16位或32位)</param> /// <returns>返回解密后字符串</returns> public static String DecryptAES(String str, string aeskey) { Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey); Byte[] toEncryptArray = Convert.FromBase64String(str); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return System.Text.UTF8Encoding.UTF8.GetString(resultArray); } #endregion #region DES加解密 /// <summary> /// 创建Key /// </summary> /// <returns></returns> public static string GenerateKey() { DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } /// <summary> /// DES加密方法一 /// </summary> /// <param name="pToEncrypt">要加密的文本</param> /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param> /// <returns></returns> public static string DESEncrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } /// <summary> /// DES解密方法一 /// </summary> /// <param name="pToDecrypt">要解密的文本</param> /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param> /// <returns></returns> public static string DESDecrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } /// <summary> /// DES加密方法二 /// </summary> /// <param name="sInputString">要加密的文本</param> /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param> /// <returns></returns> public static string EncryptString(string sInputString, string sKey) { byte[] data = Encoding.UTF8.GetBytes(sInputString); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return BitConverter.ToString(result); } /// <summary> /// DES解密方法二 /// </summary> /// <param name="sInputString">要解密的文本</param> /// <param name="sKey"></param> /// <returns>通过GenerateKey()获取(加解密必须一致)</returns> public static string DecryptString(string sInputString, string sKey) { string[] sInput = sInputString.Split("-".ToCharArray()); byte[] data = new byte[sInput.Length]; for (int i = 0; i < sInput.Length; i++) { data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber); } DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateDecryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return Encoding.UTF8.GetString(result); } #endregion #region base64加解密 /// <summary> /// base64加密 /// </summary> /// <param name="strdata">加密的明文</param> /// <returns></returns> public static string ToEncryptionBase64String(string strdata) { byte[] bytes = Encoding.UTF8.GetBytes(strdata); string str = Convert.ToBase64String(bytes); return str; } /// <summary> /// base64解密 /// </summary> /// <param name="strdata">解密的密文</param> /// <returns></returns> public static string ToDecryptBase64String(string strdata) { byte[] bytes = Convert.FromBase64String(strdata); string str = Encoding.UTF8.GetString(bytes); return str; } #endregion #region MD5加密 /// <summary> /// MD5加密(32位大写) /// </summary> /// <param name="txt">明文</param> /// <returns></returns> public static string GetMD5(string txt) { byte[] result = Encoding.Default.GetBytes(txt.Trim()); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); }
/// <summary>
/// MD5加密(32位小写)
/// </summary>
/// <param name="txt">明文</param>
/// <returns></returns>
public static string GetMD5_2(string txt)
{
byte[] result = Encoding.Default.GetBytes(txt.Trim());
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
string s = BitConverter.ToString(output).Replace("-", "");
s = s.ToLower();
return s;
}
/**/
/// <summary>
/// MD5 16位加密 加密后密码为大写
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
/**/
/// <summary>
/// MD5 16位加密 加密后密码为小写
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str2(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}
#endregion
/// <summary>
/// 标准MD5 32位小写
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string UserMd5(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x2");
}
return pwd;
}
/// <summary>
/// 标准MD5 32位大写
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string UserMd5_2(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X2");
}
return pwd;
}
}
}