#region DES加密解密 /// /// DES加密 /// /// 待加密字串 /// 32位Key值 /// 加密后的字符串 public string DESEncrypt(string strSource) { return DESEncrypt(strSource, DESKey); } public string DESEncrypt(string strSource, byte[] key) { SymmetricAlgorithm sa = Rijndael.Create(); sa.Key = key; sa.Mode = CipherMode.ECB; sa.Padding = PaddingMode.Zeros; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write); byte[] byt = Encoding.Unicode.GetBytes(strSource); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } /// /// DES解密 /// /// 待解密的字串 /// 32位Key值 /// 解密后的字符串 public string DESDecrypt(string strSource) { return DESDecrypt(strSource, DESKey); } public string DESDecrypt(string strSource, byte[] key) { SymmetricAlgorithm sa = Rijndael.Create(); sa.Key = key; sa.Mode = CipherMode.ECB; sa.Padding = PaddingMode.Zeros; ICryptoTransform ct = sa.CreateDecryptor(); byte[] byt = Convert.FromBase64String(strSource); MemoryStream ms = new MemoryStream(byt); CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs, Encoding.Unicode); return sr.ReadToEnd(); } #endregion #region 一个用hash实现的加密解密方法 /// /// 加密 /// /// /// public static string EncryptStrByHash(string src) { if (src.Length == 0) { return ""; } byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src + "Test").ToCharArray()); byte[] HaData = new byte[20]; HMACSHA1 Hmac = new HMACSHA1(HaKey); CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write); try { cs.Write(HaData, 0, HaData.Length); } finally { cs.Close(); } string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0, 16); byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray()); byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray()); byte[] EncodedBytes = { }; MemoryStream ms = new MemoryStream(); RijndaelManaged rv = new RijndaelManaged(); cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write); try { cs.Write(RiDataBuf, 0, RiDataBuf.Length); cs.FlushFinalBlock(); EncodedBytes = ms.ToArray(); } finally { ms.Close(); cs.Close(); } return HaResult + System.Convert.ToBase64String(EncodedBytes); } /// /// 解密 /// /// /// public static string DecrypStrByHash(string src) { if (src.Length < 40) return ""; byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16)); byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0, 16).ToCharArray()); byte[] InitialText = new byte[SrcBytes.Length]; RijndaelManaged rv = new RijndaelManaged(); MemoryStream ms = new MemoryStream(SrcBytes); CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read); try { cs.Read(InitialText, 0, InitialText.Length); } finally { ms.Close(); cs.Close(); } System.Text.StringBuilder Result = new System.Text.StringBuilder(); for (int i = 0; i < InitialText.Length; ++i) if (InitialText[i] > 0) Result.Append((char)InitialText[i]); return Result.ToString(); } /// /// 对加密后的密文重新编码,如果密文长>16,则去掉前16个字符,如果长度小于16,返回空字符串 /// /// /// public string ReEncryptStrByHash(string s) { string e = Encrypt.EncryptStrByHash(s); return ((e.Length > 16) ? e.Substring(16) : ""); } #endregion #region Md5加密,生成16位或32位,生成的密文都是大写 public static string Md5To16(string str) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8); t2 = t2.Replace("-", ""); return t2; } //// /// MD5 32位加密 /// /// /// public static string Md5To32(string str) { string pwd = ""; MD5 md5 = MD5.Create(); byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); for (int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString("X"); } return pwd; } #endregion #region 3DES加密解密 public string Encrypt3DES(string str) { //密钥 string sKey = "wyw308"; // //矢量,可为空 string sIV = "scf521"; // //构造对称算法 SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; mCSP.Key = Convert.FromBase64String(sKey); mCSP.IV = Convert.FromBase64String(sIV); mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV); byt = Encoding.UTF8.GetBytes(str); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } /// /// 带指定密钥和矢量的3DES加密 /// /// /// /// /// public string Encrypt3DES(string str, string sKey, string sIV) { SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; mCSP.Key = Convert.FromBase64String(sKey); mCSP.IV = Convert.FromBase64String(sIV); mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV); byt = Encoding.UTF8.GetBytes(str); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } //解密 public string Decrypt3DES(string Value) { string sKey = "wyw308"; string sIV = "scf521"; SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; mCSP.Key = Convert.FromBase64String(sKey); mCSP.IV = Convert.FromBase64String(sIV); mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV); byt = Convert.FromBase64String(Value); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Encoding.UTF8.GetString(ms.ToArray()); } /// /// 带指定密钥和矢量的3DES解密 /// /// /// /// /// public string Decrypt3DES(string str, string sKey, string sIV) { SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; mCSP.Key = Convert.FromBase64String(sKey); mCSP.IV = Convert.FromBase64String(sIV); mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV); byt = Convert.FromBase64String(str); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Encoding.UTF8.GetString(ms.ToArray()); } #endregion #region 一个简单的加密解密方法,只支持英文 public static string EnCryptEnStr(string str) //倒序加1加密 { byte[] by = new byte[str.Length]; for (int i = 0; i <= str.Length - 1; i++) { by[i] = (byte)((byte)str[i] + 1); } str = ""; for (int i = by.Length - 1; i >= 0; i--) { str += ((char)by[i]).ToString(); } return str; } public static string DeCryptEnStr(string str) //顺序减1解码 { byte[] by = new byte[str.Length]; for (int i = 0; i <= str.Length - 1; i++) { by[i] = (byte)((byte)str[i] - 1); } str = ""; for (int i = by.Length - 1; i >= 0; i--) { str += ((char)by[i]).ToString(); } return str; } #endregion #region 一个简单的加密解密方法,在上一个的基础上支持中文 public static string EnCryptCnStr(string str) { string htext = ""; // blank text for (int i = 0; i < str.Length; i++) { htext = htext + (char)(str[i] + 10 - 1 * 2); } return htext; } public static string DeCryptCnStr(string str) { string dtext = ""; for (int i = 0; i < str.Length; i++) { dtext = dtext + (char)(str[i] - 10 + 1 * 2); } return dtext; } #endregion #region Url地址编码解码 /// /// 编码Url地址 /// /// /// public static string UrlEncode(string url) { byte[] mByte = null; mByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(url); return System.Web.HttpUtility.UrlEncode(mByte); } /// /// 解码Url地址 /// /// /// public static string UrlDecode(string url) { return HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312")); } #endregion

Asp.Net 加密解密的更多相关文章

  1. ASP.Net Core 5.0 MVC AppSettings配置文件读取,Startup 类中ConfigureServices 方法、Configure 方法的使用

    配置文件读取1. 新建FirstController控制器在appsettings文件内容替换成以下代码{ &quo......

  2. 在.NET Core 中使用Quartz.NET

    Quartz.NET是功能齐全的开源作业调度系统,可用于最小的应用程序到大型企业系统。Quartz.NET具有三个主......

  3. 从零搭建一个IdentityServer——集成Asp.net core Identity

    前面的文章使用Asp.net core 5.0以及IdentityServer4搭建了一个基础的验证服务器,并实现了......

  4. ASP.NET Core 5.0 MVC中的 Razor 页面 介绍

    Razor 是一个用于将基于服务器的代码嵌入到网页中的标记语法。 Razor语法由 Razor 标记、c # 和 H......

  5. .NET的并发编程(TPL编程)是什么?

    写在前面优秀软件的一个关键特征就是具有并发性。过去的几十年,我们可以进行并发编程,但是难度很大。以前,并发性软件的编......

  6. 在.NET Core 中使用Quartz.NET

    Quartz.NET是功能齐全的开源作业调度系统,可用于最小的应用程序到大型企业系统。Quartz.NET具有三个主......

  7. ASP.NET Core错误处理中间件[4]: 响应状态码错误页面

    StatusCodePagesMiddleware中间件与ExceptionHandlerMiddleware中间件......

  8. Asp.Net 加密解密

    #region DES加密解密 /// /// DES加密 /// /// 待加密字串 /// 32位Key值 ......

  9. Asp.netCore 3.1控制器属性注入and异步事务Aop by AutoFac

    Aspect Oriented Programming(AOP)是较为热门的一个话题。AOP,国内我们都习惯称之为:......

  10. .Net Winform控件TreeView最简递归绑定方法

    前言:Treeview控件是我们在WinForm、WebForm开发中经常使用的控件,需要从数据库动态加载数据,然后......

随机推荐

  1. MySQL中外键的创建、约束以及删除

    前言在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束。外键的使用条件:1.两个表必须是In......

  2. Java Object类 和 String类 常见问答

    Java常见对象 Object类 和 String类 常见问答 6k字+总结写在最前面这个项目是从20年末就立好的 ......

  3. JavaScript this关键字的深入详解

    一、前言this关键字是JavaScript中最复杂的机制之一。它是一个很特别的关键字,被自动定义在所有函数的作用域......

  4. Python jieba库分词模式实例用法

    在中文分词中,jiebe库是最为常见的,主要的原因还是它独特的支持分词模式如:精确模式、全模式、搜索引擎模式。也对应......

  5. python用分数表示矩阵的方法实例

    前言在机器学习中,我们会经常和矩阵打交道。在矩阵的运算中,python默认的输出是浮点数,但是如果我们想要矩阵的元素......

  6. 详解java中static关键词的作用

    在java中,static是一个修饰符,用于修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序......

  7. 利用python绘制正态分布曲线

    使用Python绘制正态分布曲线,借助matplotlib绘图工具;#-*-coding:utf-8-*-"......

  8. Vue实现一种简单的无限循环滚动动画的示例

    本文主要介绍了Vue实现一种简单的无限循环滚动动画的示例,分享给大家,具体如下:先看实现效果:这种类似轮播的效果,通......

  9. MySQL全面瓦解16:存储过程相关

    概述大多数SQL语句都是针对一个或多个表的单条语句。但并非所有业务都这么简单,经常会有复杂的操作需要多条语句才能完成......

  10. 用Python自动清理电脑内重复文件,只要10行代码(自动脚本)

    给定一个文件夹,使用Python检查给定文件夹下有无文件重复,若存在重复则删除主要涉及的知识点有:os模块综合应用g......