about meWedia Ajax
给密码加密 ... Bing ... ASP.NET ... 0 /1226 ... 2 years 3 months ago
今天您上您个人Blog的时候,突然间发现上面的数据一塌糊涂,还以为进错网站了,然后您仔细认真地检查网址的每一个地址,结果准确无误,您伤心了,此时您发现数据居然没有备份,您更是绝望透顶,一头雾水。要明白数据为什么被更改,也不是很轻松就能说的明白的事。可能是数据库的问题,当然如果您确定这个没问题的话,那么最有可能的就是别人成功的进入了您的后台管理系统,这方面对于头脑灵活的人总是有好的解决方式:他们能成功利用程序漏洞。

考虑一下下面的程序:
SELECT name,password FROM Users WHERE name='@name' AND password='@password'

现在我们可以在不知道密码和用户名的情况下让该段程序运行。比如我们让@name=wubing' OR ''=',然后@password可以随便输入,因为程序根本还没有运行到password=@password,就返回True了。想想,如果没有代码再次确认身份,我们就这样很轻松的进入了。一般我们会再次设置一遍身份验证。以确保是合法用户,而不是黑客或者搞破坏的人。

现在如果我给大家一种给密码加密的方式,这样可以避免绝大多数的专业人士,当然不是所有。我将其写成了一个类,供大家参考。听说这个加密方式现在已经被人破解了,但是我想还是能比较好用的吧,有多少人能是大数学家毕业的呢?

using System;
using System.Web.Security;
using System.IO;
using System.Security.Cryptography;

<summary>
/// SSLEncrypt is used to encrpt the codes to prompt the security of the website;
/// </summary>
public class SSLEncrypt
{
private DESCryptoServiceProvider _key;

public SSLEncrypt()
{
_key = new DESCryptoServiceProvider();
}
//This method will encrypt the string and then return the encrypted string
// Encrypt the string.
public Byte[] Encrypt(string PlainText)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();

Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, _key.CreateEncryptor(), CryptoStreamMode.Write);

Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);

Write the plaintext to the stream.
sw.Writeline(PlainText);

Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();

Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();

Close the memory stream.
ms.Close();

Return the encrypted byte array.
return buffer;
}

Decrypt the byte array.
public String Decrypt(byte[] CypherText)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);

Create a CryptoStream using the memory stream and the
// CSP DES _key.
CryptoStream encStream = new CryptoStream(ms, _key.CreateDecryptor(), CryptoStreamMode.Read);

Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);

Read the stream as a string.
string val = sr.Readline();

Close the streams.
sr.Close();
encStream.Close();
ms.Close();

return val;
}
}



有这个类后,就可以创建该类的一个实例,然后调用上面的Encrypt和Dcrypt函数了,前面一个是加密,后面一个是解密。但要正确解密,一定要使用同一个实例。上次在ASP.NET中看到了连个新的用来保证网络安全的加密方式。很可惜,鄙人能力有限,并不能看懂,所以以后等我花时间看懂后再进行讲解。




Name*
Email
Website
BoldItalicUnderlineJustify LeftJustify CenterJustify RightIndentOutdentBulled ListNumbered ListInsert LineCreate LinkUnlinkInsert Face
Submit