一、引用
using System.Security.Cryptography;
using System.Text;
二、方法使用
1.MD5 16位加密
1 /**//// <summary> 2 /// MD5 16位加密 加密后密码为小写 3 /// </summary> 4 /// <param name="ConvertString"></param> 5 /// <returns></returns> 6 public static string GetMd5Str(string ConvertString) 7 { 8 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 9 string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8); 10 t2 = t2.Replace("-", ""); 11 12 t2 = t2.ToLower(); 13 14 return t2; 15 }
2. MD5 32位加密
/**//// <summary> /// MD5 32位加密 /// </summary> /// <param name="str"></param> /// <returns></returns> 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("X"); } return pwd; }
3.C# Base64编码解码 ,Md5、Rsa加密解密
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace ClassLibrary1 { class Code { //1、base64 to string public static string Base64Encrypt(string strPath) { byte[] bpath = Convert.FromBase64String(strPath); string returnData = Encoding.Default.GetString(bpath); return returnData; } //2、string to base64 public static string Base64DesEncrypt(string strPath) { Encoding encode = Encoding.ASCII; byte[] bytedata = encode.GetBytes(strPath); string returnData = Convert.ToBase64String(bytedata, 0, bytedata.Length); return returnData; } /// <summary> /// 对字符串进MD5加密 /// </summary> /// <param name="sourceStr">源类型</param> /// <returns>加密后字符串</returns> public static string Md5Encrypt(string sourceStr) { MD5 md5 = new MD5CryptoServiceProvider(); //将要加密的字符串转换成字节数组 byte[] palindata = Encoding.Default.GetBytes(sourceStr); //通过字节数组进行加密 byte[] encryptdata = md5.ComputeHash(palindata); //将加密后的字节数组转换成字符串 string returnData = Convert.ToBase64String(encryptdata); return returnData; } /// <summary> /// Md5加密 /// </summary> /// <param name="pToEncrypt">要加密的string</param> /// <param name="sKey">要加密的key</param> /// <returns></returns> public static string Md5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.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); } var s = ret.ToString(); return s; } /// <summary> /// Md5解密 /// </summary> /// <param name="pToDecrypt">解密string</param> /// <param name="sKey">解密key(要8位数)</param> /// <returns></returns> public static string Md5Decrypt(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 = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } public static CspParameters Param; /// <summary> /// 进行 RSA 加密 /// </summary> /// <param name="sourceStr">源字符串</param> /// <returns>加密后字符串</returns> public static string RsaEncrypt(string sourceStr) { Param = new CspParameters(); //密匙容器的名称,保持加密解密一致才能解密成功 Param.KeyContainerName = "Navis"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) { //将要加密的字符串转换成字节数组 byte[] plaindata = Encoding.Default.GetBytes(sourceStr); //通过字节数组进行加密 byte[] encryptdata = rsa.Encrypt(plaindata, false); //将加密后的字节数组转换成字符串 return Convert.ToBase64String(encryptdata); } } /// <summary> /// 通过RSA 加密方式进行解密 /// </summary> /// <param name="codingStr">加密字符串</param> /// <returns>解密后字符串</returns> public static string RsaDesEncrypt(string codingStr) { Param = new CspParameters(); Param.KeyContainerName = "Navis"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) { byte[] encryptdata = Convert.FromBase64String(codingStr); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return Encoding.Default.GetString(decryptdata); } } } }