首页 > 代码库 > C# 加密解密

C# 加密解密

  1 using System;  2 using System.IO;  3 using System.Security.Cryptography;  4 using System.Text;  5   6 namespace Public  7 {  8     /// <summary>  9     /// 加密解密 10     /// </summary> 11     public class EncryptUtils 12     { 13         #region Sha256加密 14         /// <summary> 15         /// Sha256加密算法 16         /// </summary> 17         /// <param name="plainText"></param> 18         /// <returns></returns> 19         public static string Sha256(string plainText) 20         { 21             SHA256Managed _sha256 = new SHA256Managed(); 22             byte[] _cipherText = _sha256.ComputeHash(Encoding.Default.GetBytes(plainText)); 23             return Convert.ToBase64String(_cipherText); 24         } 25         #endregion 26  27         #region Base64加密解密 28         /// <summary> 29         /// Base64加密 30         /// </summary> 31         /// <param name="input">需要加密的字符串</param> 32         /// <returns></returns> 33         public static string Base64Encrypt(string input) 34         { 35             return Base64Encrypt(input, new UTF8Encoding()); 36         } 37  38         /// <summary> 39         /// Base64加密 40         /// </summary> 41         /// <param name="input">需要加密的字符串</param> 42         /// <param name="encode">字符编码</param> 43         /// <returns></returns> 44         public static string Base64Encrypt(string input, Encoding encode) 45         { 46             return Convert.ToBase64String(encode.GetBytes(input)); 47         } 48  49         /// <summary> 50         /// Base64解密 51         /// </summary> 52         /// <param name="input">需要解密的字符串</param> 53         /// <returns></returns> 54         public static string Base64Decrypt(string input) 55         { 56             return Base64Decrypt(input, new UTF8Encoding()); 57         } 58  59         /// <summary> 60         /// Base64解密 61         /// </summary> 62         /// <param name="input">需要解密的字符串</param> 63         /// <param name="encode">字符的编码</param> 64         /// <returns></returns> 65         public static string Base64Decrypt(string input, Encoding encode) 66         { 67             return encode.GetString(Convert.FromBase64String(input)); 68         } 69         #endregion 70  71         #region DES加密解密 72         /// <summary> 73         /// DES加密 74         /// </summary> 75         /// <param name="data">加密数据</param> 76         /// <returns></returns> 77         public static string DESEncrypt(string data) 78         { 79             return DESEncrypt(data, "znstkxun", "xxuntian"); 80         } 81         /// <summary> 82         /// DES加密 83         /// </summary> 84         /// <param name="data">加密数据</param> 85         /// <param name="key">8位字符的密钥字符串</param> 86         /// <param name="iv">8位字符的初始化向量字符串</param> 87         /// <returns></returns> 88         public static string DESEncrypt(string data, string key, string iv) 89         { 90             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key); 91             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv); 92  93             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); 94             int i = cryptoProvider.KeySize; 95             MemoryStream ms = new MemoryStream(); 96             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); 97  98             StreamWriter sw = new StreamWriter(cst); 99             sw.Write(data);100             sw.Flush();101             cst.FlushFinalBlock();102             sw.Flush();103             return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);104         }105         /// <summary>106         /// DES解密107         /// </summary>108         /// <param name="data">加密数据</param>109         /// <returns></returns>110         public static string DESDecrypt(string data)111         {112             return DESDecrypt(data, "znstkxun", "xxuntian");113         }114         /// <summary>115         /// DES解密116         /// </summary>117         /// <param name="data">解密数据</param>118         /// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>119         /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>120         /// <returns></returns>121         public static string DESDecrypt(string data, string key, string iv)122         {123             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);124             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);125 126             byte[] byEnc;127             try128             {129                 byEnc = Convert.FromBase64String(data);130             }131             catch (Exception ex)132             {133                 return "数据解密失败!\n 失败原因:" + ex.Message;134             }135 136             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();137             MemoryStream ms = new MemoryStream(byEnc);138             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);139             StreamReader sr = new StreamReader(cst);140             return sr.ReadToEnd();141         }142         #endregion143 144         #region MD5加密145         /// <summary>146         /// MD5加密147         /// </summary>148         /// <param name="input">需要加密的字符串</param>149         /// <returns></returns>150         public static string MD5Encrypt(string input)151         {152             return MD5Encrypt(input, new UTF8Encoding());153         }154 155         /// <summary>156         /// MD5加密157         /// </summary>158         /// <param name="input">需要加密的字符串</param>159         /// <param name="encode">字符的编码</param>160         /// <returns></returns>161         public static string MD5Encrypt(string input, Encoding encode)162         {163             MD5 md5 = new MD5CryptoServiceProvider();164             byte[] t = md5.ComputeHash(encode.GetBytes(input));165             StringBuilder sb = new StringBuilder(32);166             for (int i = 0; i < t.Length; i++)167                 sb.Append(t[i].ToString("x").PadLeft(2, 0));168             return sb.ToString();169         }170 171         /// <summary>172         /// MD5对文件流加密173         /// </summary>174         /// <param name="sr"></param>175         /// <returns></returns>176         public static string MD5Encrypt(Stream stream)177         {178             MD5 md5serv = MD5CryptoServiceProvider.Create();179             byte[] buffer = md5serv.ComputeHash(stream);180             StringBuilder sb = new StringBuilder();181             foreach (byte var in buffer)182                 sb.Append(var.ToString("x2"));183             return sb.ToString();184         }185 186         /// <summary>187         /// MD5加密(返回16位加密串)188         /// </summary>189         /// <param name="input"></param>190         /// <param name="encode"></param>191         /// <returns></returns>192         public static string MD5Encrypt16(string input, Encoding encode)193         {194             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();195             string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8);196             result = result.Replace("-", "");197             return result;198         }199         #endregion200 201         #region 3DES 加密解密202 203         public static string DES3Encrypt(string data, string key)204         {205             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();206 207             DES.Key = ASCIIEncoding.ASCII.GetBytes(key);208             DES.Mode = CipherMode.CBC;209             DES.Padding = PaddingMode.PKCS7;210 211             ICryptoTransform DESEncrypt = DES.CreateEncryptor();212 213             byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data);214             return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));215         }216 217         public static string DES3Decrypt(string data, string key)218         {219             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();220 221             DES.Key = ASCIIEncoding.ASCII.GetBytes(key);222             DES.Mode = CipherMode.CBC;223             DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;224 225             ICryptoTransform DESDecrypt = DES.CreateDecryptor();226 227             string result = "";228             try229             {230                 byte[] Buffer = Convert.FromBase64String(data);231                 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));232             }233             catch (Exception ex)234             {235                 result = "数据解密失败!\n 失败原因:" + ex.Message;236             }237             return result;238         }239 240         #endregion241     }242 }

 

C# 加密解密