asp.net 下!密码加密!
重谢!求源代码!
重谢!求源代码!
解决方案 »
- SQL2000连接问题 急
- 使用 StateServer 保存 Session,谁用过啊?
- 正则表达式高手来,寻求帮助
- 如何在gridview或listview这样的控件中单机鼠标取得对应行号?
- 调用存储过程这些写 参数值怎么没有传入啊????
- 关于ASP.NET 2.0 操作GridView自带的删除、插入、更新等按钮时如何发出提问
- 如何用C# 通过ldap进行访问,高手进
- 请教几个显示的问题
- 寻求application center test使用方法,如:实例
- web打印哪种方式好??
- VS.net2003提示Automation服务器无法创建对象
- 寻求解决方案:不公平的.net自定义控件(高手请进)
××××××××××××××××××××××××××××××××××××××
Imports System.Web.SecurityPublic Class WebForm2
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "用户名为:"
Label1.Text &= FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "md5") Label2.Text = "密码为:"
Label2.Text &= FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "md5")
End Sub
End Class
×××××××××××××××××××××××××××××××××××××××
前台:
×××××××××××××××××××××××××××××××××××××××
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table height="100%" width="100%">
<tr>
<td vAlign="middle" align="center">
<table>
<tr vAlign="middle" align="center">
<td>名字:</td>
<td><asp:textbox id="TextBox1" runat="server"></asp:textbox></td>
</tr>
<tr vAlign="middle" align="center">
<td>密码:</td>
<td><asp:textbox id="TextBox2" runat="server"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="center" colSpan="2"><asp:button id="Button1" Text="计算" Runat="server"></asp:button></td>
</tr>
<tr>
<td vAlign="middle" align="center" colSpan="2"><asp:label id="Label1" Runat="server"></asp:label></td>
</tr>
<tr>
<td colspan="2" vAlign="middle" align="center"><asp:Label ID="Label2" Runat="server" /></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>
×××××××××××××××××××××××××××××××××××××××
using System.IO;
using System.Security.Cryptography;
using System.Text;namespace ydoo.Common
{
public class SymmetricMethod
{
private SymmetricAlgorithm mobjCryptoService;
private string Key; /// <summary>
/// 对称加密类的构造函数
/// </summary>
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key = "uyodfgawerpouwr[yilk3450927834asdojlk212=-0retwertwertyw"; // 任意字符串
} /// <summary>
/// 获得密钥
/// </summary>
/// <returns>密钥</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
} /// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns>初试向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk"; // 任意字符串
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
} /// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>经过加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
} /// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>经过解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
}调用应该没问题吧