学习这一块内容前,先得大概知道"哈希算法"和"对称加密算法"是咋回事儿.

不清楚的先去这里充电5分钟再回来

哈希算法------http://baike.baidu.com/view/273836.htm

对称加密算法--http://baike.baidu.com/view/7591.htm

使用步骤:

1.先添加Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll的引用

2.参照http://www.cnblogs.com/Terrylee/archive/2005/11/29/286688.html中所写,创建web.config中的相关节点

Enterprise Library 4.1学习笔记6----加密应用程序块

需要注意的是:
对称算法中的密钥文本文件,在asp.net环境中一定不要存放在网站目录下,否则谁都可以下载,形同虚设!

完成后,web.config内容大概如下:

Enterprise Library 4.1学习笔记6----加密应用程序块
<configuration>
    
<configSections>
        Enterprise Library 4.1学习笔记6----加密应用程序块
        
<section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=4.1.0.0, Culture=neutral"/>
        Enterprise Library 4.1学习笔记6----加密应用程序块
    
</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>
    Enterprise Library 4.1学习笔记6----加密应用程序块

3.接下来就可以测试了,关键地方已经加了注释
 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>
运行界面图:
Enterprise Library 4.1学习笔记6----加密应用程序块

相关文章:

  • 2021-05-28
  • 2021-10-14
  • 2021-12-12
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2021-06-04
  • 2021-07-28
猜你喜欢
  • 2021-12-11
  • 2021-08-10
  • 2022-01-26
  • 2022-12-23
  • 2021-09-02
  • 2021-08-03
相关资源
相似解决方案