在 .NET Framework 中,RNGCryptoServiceProvider 是随机数生成器算法的实现,对于数据算法,.NET Framework 则在其它命名空间中实现,如 Convert 类实现 Base 64 编码,System.Text 来实现编码方式的转换等。
从以上来看,.NET Framework 对于数据加密/编码还是支持比较好,大大地方便了开发人员,但美中不足的是,.NET Framework 中的数据加密算法仍然不够完全,如 IDEA、BLOWFISH、其它算法,如ElGamal、Deffie-Hellman、ECC 等,对于一些其它的数据校验算法支持也不够,如 CRC、SFV 等,开发人员只能去从早期代码做移植或者寻找第三方厂商的实现。
下面本就简单介绍在项目中常用的加密及解密的方法
一、MD5加密算法
[.NET类库中自带的算法 MD5是个不可逆的算法 没有解密的算法]
其实在ASP.Net编程中加密数据。在DotNet中有自带的类:
System.Web.Security.HashPasswordForStoringInConfigFile() public string md5(string str,int code) { if(code==16) //16位MD5加密(取32位加密的9~25字符) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5") .ToLower().Substring(8,16) ; } if(code==32) //32位加密 { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5") .ToLower(); } return "00000000000000000000000000000000"; }
简单的使用:
//--导入所需要的包
using System.IO;
using System.Text;
using System.Security.Cryptography;
(1)MD5普通加密
//获取要加密的字段,并转化为Byte[]数组
byte[] data = System.Text.Encoding.Unicode
.GetBytes(TextBox1.Text.ToCharArray());
//建立加密服务
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
//加密Byte[]数组
byte[] result = md5.ComputeHash(data);
Label1.Text = "MD5普通加密:" + System.Text.Encoding.Unicode.GetString(result);
(2)MD5密码加密[常用]
Label1.Text = "MD5密码加密:" + System.Web.Security.FormsAuthentication
.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");
(3)ASP.NET中加密与解密QueryString的方法[常用]
//加密
Response.Redirect("DetailInfo.aspx?id=" + Convert.ToBase64String
(System.Text.Encoding.Default.GetBytes("whaben")).Replace("+","%2B"));
//解密
string ID = System.Text.Encoding.Default.GetString
(Convert.FromBase64String(Request.QueryString["id"].ToString().Replace("%2B","+")));
二、DES加密及解密的算法[常用密钥算法
简单的使用:
//--导入所需要的包 using System.IO; using System.Text; using System.Security.Cryptography; 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()); }
三、RSA加密及解密的算法[常用密钥算法
简单的使用:
//--导入所需要的包 using System.Text; using System.Security.Cryptography; //加密算法 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); }
您可能感兴趣的文章: