学习这一块内容前,先得大概知道"哈希算法"和"对称加密算法"是咋回事儿.
不清楚的先去这里充电5分钟再回来
哈希算法------http://baike.baidu.com/view/273836.htm
对称加密算法--http://baike.baidu.com/view/7591.htm
使用步骤:
1.先添加Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll的引用
2.参照javascript:void(0)中所写,创建web.config中的相关节点
需要注意的是:
对称算法中的密钥文本文件,在asp.net环境中一定不要存放在网站目录下,否则谁都可以下载,形同虚设!
完成后,web.config内容大概如下:
<configuration>
<configSections>
<section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=4.1.0.0, Culture=neutral"/>
</configSections>
<securityCryptographyConfiguration>
<hashProviders>
<add algorithmType="System.Security.Cryptography.SHA256Managed, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" saltEnabled="true" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=4.1.0.0, Culture=neutral" name="SHA256Managed"/>
</hashProviders>
<symmetricCryptoProviders>
<add algorithmType="System.Security.Cryptography.RC2CryptoServiceProvider, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" protectedKeyFilename="C:\key.txt" protectedKeyProtectionScope="LocalMachine" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=4.1.0.0, Culture=neutral" name="RC2CryptoServiceProvider"/>
</symmetricCryptoProviders>
</securityCryptographyConfiguration>
3.接下来就可以测试了,关键地方已经加了注释
Code
using System;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace EncryptTest
{
public partial class _Default : System.Web.UI.Page
{
//这二个值必须与web.config相关Providers中的name一致
private const string hashProvider = "SHA256Managed";
private const string symmProvider = "RC2CryptoServiceProvider";
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 生成Hash散列值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnHash_Click(object sender, EventArgs e)
{
byte[] valueToHash = Encoding.Default.GetBytes(txtOriginal.Text);
byte[] generatedHash = Cryptographer.CreateHash(hashProvider, valueToHash);
// Clear the byte array memory
Array.Clear(valueToHash, 0, valueToHash.Length);
lHash.Text = Convert.ToBase64String(generatedHash);
}
/// <summary>
/// 比较Hash散列值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCompare_Click(object sender, EventArgs e)
{
byte[] valueToHash = System.Text.Encoding.UTF8.GetBytes(txtCompare.Text);
if (lHash.Text != "")
{
bool matched = Cryptographer.CompareHash(hashProvider, valueToHash, Convert.FromBase64String(lHash.Text));
// Clear the byte array memory
Array.Clear(valueToHash, 0, valueToHash.Length);
if (matched)
{
lCompare.Text = "<span style='color:green'>该字符串未被篡改过!</span>";
}
else
{
lCompare.Text = "<span style='color:red'>该字符串被修改过了!</span>";
}
}
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEncrypt_Click(object sender, EventArgs e)
{
lEncrypt.Text = Cryptographer.EncryptSymmetric(symmProvider, txtOriginal2.Text);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDecrypt_Click(object sender, EventArgs e)
{
lDecrypt.Text = Cryptographer.DecryptSymmetric(symmProvider, lEncrypt.Text);
}
}
}
前端asp页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EncryptTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
*
{
font-size: 12px;
line-height: 18px;
}
th
{
font-size: 14px;
line-height: 20px;
}
.btn
{
height: 21px;
width: 100px;
line-height: 16px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<th colspan="3" align="center">
Hash算法测试
</th>
</tr>
<tr>
<td align="right">
输入要加密的文字:
</td>
<td>
<asp:TextBox ID="txtOriginal" runat="server" Width="300px"></asp:TextBox>
</td>
<td align="left">
<asp:Button ID="btnHash" runat="server" Text="生成Hash散列" OnClick="btnHash_Click" CssClass="btn" />
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Literal ID="lHash" runat="server"></asp:Literal>
</td>
</tr>
<tr>
<td align="right">
请输入要比较的文字:
</td>
<td>
<asp:TextBox ID="txtCompare" runat="server" Width="300px"></asp:TextBox>
</td>
<td align="left">
<asp:Button ID="btnCompare" runat="server" Text="比较Hash散列" OnClick="btnCompare_Click"
CssClass="btn" />
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Literal ID="lCompare" runat="server"></asp:Literal>
</td>
</tr>
<tr>
<th colspan="3" align="center">
对称加密算法测试
</th>
</tr>
<tr>
<td align="right">
输入要加密的文字:
</td>
<td>
<asp:TextBox ID="txtOriginal2" runat="server" Width="300px"></asp:TextBox>
</td>
<td align="left">
<asp:Button ID="btnEncrypt" runat="server" Text=" 加 密 " CssClass="btn" OnClick="btnEncrypt_Click" />
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Literal ID="lEncrypt" runat="server"></asp:Literal>
</td>
</tr>
<tr>
<td align="right">
输入要解密的密码:
</td>
<td>
<asp:TextBox ID="txtEncrypt" runat="server" Width="300px"></asp:TextBox>
</td>
<td align="left">
<asp:Button ID="btnDecrypt" runat="server" Text=" 解 密 " CssClass="btn"
onclick="btnDecrypt_Click"/>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Literal ID="lDecrypt" runat="server"></asp:Literal>
</td>
</tr>
</table>
</form>
</body>
</html>
运行界面图:
作者:菩提树下的杨过