当前位置:
首页 > Python基础教程 >
-
C#教程之MD5密码加密
using System; using System.Security.Cryptography; using System.Text; namespace DimoNetwork.Common.DEncrypt { public enum MD5ResultMode : byte { Strong = 0, Weak = 1 } /// <summary> /// 在应用程序中定义用于单向加密文本的方法 /// </summary> public class TextEncrypt { private TextEncrypt() { } #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text, "DimoNet"); } /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.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); } return ret.ToString(); } /// <summary> /// MD5 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string MD5EncryptPassword(string password, int? length = null) { if (password == null) { throw new ArgumentNullException("password"); } return MD5EncryptPassword(password, MD5ResultMode.Strong, length); } /// <summary> /// MD5 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <param name="mode">加密强度</param> /// <returns></returns> public static string MD5EncryptPassword(string password, MD5ResultMode mode, int? length = null) { if (password == null) { throw new ArgumentNullException("password"); } MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password))); if (length != null && length == 16) { str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)), 4, 8); } provider.Clear(); if (mode != MD5ResultMode.Strong) { return str.Replace("-", null).Substring(8, 0x10); } return str.Replace("-", null); } #endregion #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text, "DimoNet"); } /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = Text.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { // i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); i = System.Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion /// <summary> /// Base64 解码 /// </summary> /// <param name="message"></param> /// <returns></returns> public static string Base64Decode(string message) { byte[] bytes = Convert.FromBase64String(message); return Encoding.UTF8.GetString(bytes); } /// <summary> /// Base64 编码 /// </summary> /// <param name="message"></param> /// <returns></returns> public static string Base64Encode(string message) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(message)); } /// <summary> /// DSA 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string DSAEncryptPassword(string password) { if (password == null) { throw new ArgumentNullException("password"); } DSACryptoServiceProvider provider = new DSACryptoServiceProvider(); string str = BitConverter.ToString(provider.SignData(Encoding.UTF8.GetBytes(password))); provider.Clear(); return str.Replace("-", null); } /// <summary> /// MD5 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string EncryptPassword(string password) { if (password == null) { throw new ArgumentNullException("password"); } return MD5EncryptPassword(password); } /// <summary> /// MD5 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string MD5EncryptPassword(string password) { if (password == null) { throw new ArgumentNullException("password"); } return MD5EncryptPassword(password, MD5ResultMode.Strong); } /// <summary> /// MD5 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <param name="mode">加密强度</param> /// <returns></returns> public static string MD5EncryptPassword(string password, MD5ResultMode mode) { if (password == null) { throw new ArgumentNullException("password"); } MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password))); provider.Clear(); if (mode != MD5ResultMode.Strong) { return str.Replace("-", null).Substring(8, 0x10); } return str.Replace("-", null); } /// <summary> /// SHA1 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string SHA1EncryptPassword(string password) { if (password == null) { throw new ArgumentNullException("password"); } SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider(); string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password))); provider.Clear(); return str.Replace("-", null); } /// <summary> /// SHA256 加密 /// </summary> /// <param name="password">要加密的字符串</param> /// <returns></returns> public static string SHA256(string password) { byte[] bytes = Encoding.UTF8.GetBytes(password); SHA256Managed managed = new SHA256Managed(); return Convert.ToBase64String(managed.ComputeHash(bytes)); } } }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式