博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net 的加密Encode和解密Decode.
阅读量:5310 次
发布时间:2019-06-14

本文共 3917 字,大约阅读时间需要 13 分钟。

asp.net 的加密和解密(c#版):

1. 导入所需包:

using System.IO;using System.Text;using System.Security.Cryptography;

2.加密

  1)MD5普通加密  

//获取要加密的字段,并转化为Byte[]数组byte[] data = System.Text.Encoding.Unicode .GetBytes(str.ToCharArray());//建立加密服务System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();//加密Byte[]数组byte[] result = md5.ComputeHash(data);response.write( "MD5普通加密:" +System.Text.Encoding.Unicode.GetString(result));

 

  2)MD5密码加密[常用]  

response.write("MD5密码加密:" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"));

 

  3)ASP.NET中加密与解密QueryString的方法[常用]  

//加密Response.Redirect("DetailInfo.aspx?id=" + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(str)).Replace("+","%2B"));//解密string ID = System.Text.Encoding.Default.GetString(Convert.FromBase64String(Request.QueryString["id"].ToString().Replace("%2B","+"))); 加密和解密函数:Protected Function Encode(ByVal theStr As String) As String       Return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(theStr)).Replace("+", "%2B")End FunctionProtected Function Decode(ByVal theStr As String) As String        Return System.Text.Encoding.Default.GetString(Convert.FromBase64String(theStr.Replace("%2B", "+")))End Function

 

 

  4)DES加密及解密的算法[常用密钥算法] 

public static string Key = "DKMAB5DE";//加密密钥必须为8位    //加密算法    public static string MD5Encrypt(string pToEncrypt){        DESCryptoServiceProvider des = new DESCryptoServiceProvider();      byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);      des.Key = ASCIIEncoding.ASCII.GetBytes(Key);      des.IV = ASCIIEncoding.ASCII.GetBytes(Key);      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);      }      ret.ToString();     return ret.ToString();    }    //解密算法    public static string MD5Decrypt(string pToDecrypt){      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 = ASCIIEncoding.ASCII.GetBytes(Key);    des.IV = ASCIIEncoding.ASCII.GetBytes(Key);    MemoryStream ms = new MemoryStream();    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);    cs.Write(inputByteArray, 0, inputByteArray.Length);    cs.FlushFinalBlock();    StringBuilder ret = new StringBuilder();    return System.Text.Encoding.ASCII.GetString(ms.ToArray());  }

 

  5)RSA加密及解密的算法[常用密钥算法]   

//加密算法public string RSAEncrypt(string encryptString) {     CspParameters csp = new CspParameters();    csp.KeyContainerName = "whaben";    RSACryptoServiceProvider RSAProvider = new RSACryptoServiceProvider(csp);    byte[] encryptBytes = RSAProvider.Encrypt(ASCIIEncoding.ASCII.GetBytes(encryptString), true);    string str = "";     foreach (byte b in encryptBytes){
   str = str + string.Format("{0:x2}", b);   }   return str;}//解密算法public string RSADecrypt(string decryptString){
 CspParameters csp = new CspParameters();  csp.KeyContainerName = "whaben";  RSACryptoServiceProvider RSAProvider = new RSACryptoServiceProvider(csp);  int length = (decryptString.Length / 2);  byte[] decryptBytes = new byte[length];  for (int index = 0; index < length; index++){
     string substring = decryptString.Substring(index * 2, 2);    decryptBytes[index] = Convert.ToByte(substring, 16);  }  decryptBytes = RSAProvider.Decrypt(decryptBytes, true);  return ASCIIEncoding.ASCII.GetString(decryptBytes);}

 

 

转载于:https://www.cnblogs.com/nancyzhang/p/3725614.html

你可能感兴趣的文章
开源网络漏洞扫描软件
查看>>
yum 命令跳过特定(指定)软件包升级方法
查看>>
创新课程管理系统数据库设计心得
查看>>
Hallo wolrd!
查看>>
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
Chapter 3 Phenomenon——12
查看>>
和小哥哥一起刷洛谷(1)
查看>>
遇麻烦,Win7+Ubuntu12.10+Archlinux12.10 +grub
查看>>
SqlBulkCopy大批量导入数据
查看>>
pandas 修改指定列中所有内容
查看>>
「 Luogu P2285 」打鼹鼠
查看>>
lua语言入门之Sublime Text设置lua的Build System
查看>>
vue.js基础
查看>>
电脑的自带图标的显示
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
C++ 删除字符串的两种实现方式
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Java抽象类和接口的比较
查看>>
开发进度一
查看>>