學習這一塊内容前,先得大概知道"雜湊演算法"和"對稱加密算法"是咋回事兒.
不清楚的先去這裡充電5分鐘再回來
雜湊演算法------http://baike.baidu.com/view/273836.htm
對稱加密算法--http://baike.baidu.com/view/7591.htm
使用步驟:
1.先添加Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll的引用
需要注意的是:
對稱算法中的密鑰文本檔案,在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);
/// 比較Hash散列值
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>";
}
/// 加密
protected void btnEncrypt_Click(object sender, EventArgs e)
lEncrypt.Text = Cryptographer.EncryptSymmetric(symmProvider, txtOriginal2.Text);
/// 解密
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>
<td align="right">
輸入要加密的文字:
</td>
<td>
<asp:TextBox ID="txtOriginal" runat="server" Width="300px"></asp:TextBox>
<td align="left">
<asp:Button ID="btnHash" runat="server" Text="生成Hash散列" OnClick="btnHash_Click" CssClass="btn" />
<td colspan="3" align="center">
<asp:Literal ID="lHash" runat="server"></asp:Literal>
請輸入要比較的文字:
<asp:TextBox ID="txtCompare" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnCompare" runat="server" Text="比較Hash散列" OnClick="btnCompare_Click"
CssClass="btn" />
<asp:Literal ID="lCompare" runat="server"></asp:Literal>
對稱加密算法測試
<asp:TextBox ID="txtOriginal2" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnEncrypt" runat="server" Text=" 加 密 " CssClass="btn" OnClick="btnEncrypt_Click" />
<asp:Literal ID="lEncrypt" runat="server"></asp:Literal>
輸入要解密的密碼:
<asp:TextBox ID="txtEncrypt" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnDecrypt" runat="server" Text=" 解 密 " CssClass="btn"
onclick="btnDecrypt_Click"/>
<asp:Literal ID="lDecrypt" runat="server"></asp:Literal>
</table>
</form>
</body>
</html>
運作界面圖: