开发手册 欢迎您!
软件开发者资料库

.NET Core(C#) 常用字符串加解密工具类(MD5,DES,AES,Base64,SHA256)

本文主要介绍.NET Core(C#)中,常用字符串加解密(MD5,DES,AES,Base64,SHA256)的工具类方法,以及相关的示例代码。

1、加密解密工具类(MD5,DES,AES,Base64,SHA256)

using System;using System.IO;using System.Security.Cryptography;using System.Text;namespace wonhero.helper{    ///     /// 加、解密帮助类    ///     public static class Cryptography    {        ///         /// 计算32位MD5        ///         /// 需要计算的字符串        /// 32位的MD5值        public static string ToMd5(this string str)        {            var bytes = Encoding.UTF8.GetBytes(str);            return bytes.ToMd5();        }        public static string ToMd5(this byte[] bytes)        {#if !NETSTANDARD            var md5 = new MD5CryptoServiceProvider();#elsevar md5 = MD5.Create();#endif            var targetData = md5.ComputeHash(bytes);            return BitConverter.ToString(targetData).Replace("-", "").ToLower();        }        ///         /// 计算8位MD5        ///         /// 需要计算的字符串        /// 8位的MD5值        public static string ToShortMd5(this string str)        {            return ToMd5(str).Substring(8, 16).ToLower();        }        ///         /// DES加密        ///         /// 秘钥        /// 需要加密的字符串        /// 加密后的字符串        public static string ToDes(string key, string str)        {            var cryptoProvider = new DESCryptoServiceProvider();            var bytes = Encoding.ASCII.GetBytes(key);            var crypto = cryptoProvider.CreateEncryptor(bytes, bytes);            var ms = new MemoryStream();            var cst = new CryptoStream(ms, crypto, CryptoStreamMode.Write);            var sw = new StreamWriter(cst);            sw.Write(str);            sw.Flush();            cst.FlushFinalBlock();            sw.Flush();            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);        }  #region Base64加密解密        ///         /// Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。        /// 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,        /// 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的        /// 系統中而不同。        /// Base64加密        ///         ///         ///         public static string Base64Encrypt(this string str)        {            byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);            return Convert.ToBase64String(encbuff);        }        ///         /// Base64解密        ///         ///         ///         public static string Base64Decrypt(this string str)        {            byte[] decbuff = Convert.FromBase64String(str);            return System.Text.Encoding.UTF8.GetString(decbuff);        }        #endregion#region  SHA256加密算法         ///         /// SHA256函数        ///         /// 原始字符串        /// SHA256结果(返回长度为44字节的字符串)        public static string SHA256(this string str)        {            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);            SHA256Managed Sha256 = new SHA256Managed();            byte[] Result = Sha256.ComputeHash(SHA256Data);            return Convert.ToBase64String(Result);  //返回长度为44字节的字符串        }        #endregion    ///           /// AES加密算法          ///           /// 明文字符串          /// 字符串          public static string EncryptByAES(string input, string key, string iv)        {            if (string.IsNullOrWhiteSpace(input))            {                return input;            }            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())            {                rijndaelManaged.Mode = CipherMode.CBC;                rijndaelManaged.Padding = PaddingMode.PKCS7;                rijndaelManaged.FeedbackSize = 128;                rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);                rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);                ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);                using (MemoryStream msEncrypt = new MemoryStream())                {                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))                    {                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))                        {                            swEncrypt.Write(input);                        }                        byte[] bytes = msEncrypt.ToArray();                        return Convert.ToBase64String(bytes);                    }                }            }        }        ///           /// AES解密          ///           /// 密文字节数组          /// 返回解密后的字符串          public static string DecryptByAES(string input, string key, string iv)        {            if (string.IsNullOrWhiteSpace(input))            {                return input;            }            var buffer = Convert.FromBase64String(input);            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())            {                rijndaelManaged.Mode = CipherMode.CBC;                rijndaelManaged.Padding = PaddingMode.PKCS7;                rijndaelManaged.FeedbackSize = 128;                rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);                rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);                ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);                using (MemoryStream msEncrypt = new MemoryStream(buffer))                {                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))                    {                        using (StreamReader srEncrypt = new StreamReader(csEncrypt))                        {                            return srEncrypt.ReadToEnd();                        }                    }                }            }        }    }}

2、使用示例代码

"www.wonhero.com".ToMd5();"www.wonhero.com".ToShortMd5();Cryptography.ToDes("wonhero","hello world!!!");"www.wonhero.com".Base64Encrypt();"www.wonhero.com".SHA256();Cryptography.EncryptByAES("137", "CJAVAPmXcuAksWmF", "V33CQ1428SI8ZNMT");Cryptography.DecryptByAES( "v9BxRJX+ojPDhyJFrglcbA==","CJAVAPmXcuAksWmF","V33CQ1428SI8ZNMT");

相关文档.NET Core(C#)和AES.js实现AES(Crypto)加密和解密的示例代码