asp.net中利用加密盐进行加密字符串
2006-09-22 13:46
549 查看
asp.net中提供了很多的加密算法,如:SHA1,MD5... ,同时还提供了一种加密盐(salt)机制。
加密盐就是通过salt和字符串混合加密得出的(salt称为盐,是一串随机字符串,如果salt不一样则加密出来的结果也是不一样的)
由于salt是一个随机数因此下面两个按钮中执行的结果也是不一样的!
废话少说,还是给出相应的代码:
#region ---生成加密盐
public string CreateSalt(int size)
{
//使用加密服务提供程序 (CSP) 提供的实现来实现加密随机数生成器 (RNG).
//using System.Security.Cryptography;
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
//返回Base64字符串表示形式的随机数
return Convert.ToBase64String(buff);
}
#endregion
#region ---SHA1+Salt进行加密
private string createPwdHashSHA1(string pwd, string salt)
{
string saltAndPwd = string.Concat(pwd, salt);
string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "SHA1");
return hashedPwd;
}
#endregion
#region ---MD5+Salt进行加密
private string createPwdHashMD5(string pwd, string salt)
{
string saltAndPwd = string.Concat(pwd, salt);
string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "MD5");
return hashedPwd;
}
#endregion
#region ---进行加密
protected void Button1_Click(object sender, EventArgs e)
{
string salt = this.CreateSalt(32);
Label7.Text = this.createPwdHashSHA1(TextBox1.Text, salt);//salt+sha1
Label6.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "SHA1");//sha1
Label8.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");//md5
Label9.Text = this.createPwdHashMD5(TextBox1.Text, salt);//md5+salt
}
#endregion
protected void Button2_Click(object sender, EventArgs e)
{
string salt = this.CreateSalt(32);
Label11.Text = this.createPwdHashSHA1(TextBox1.Text, salt);//salt+sha1
Label10.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "SHA1");//sha1
Label12.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");//md5
Label13.Text = this.createPwdHashMD5(TextBox1.Text, salt);//md5+salt
}
加密盐就是通过salt和字符串混合加密得出的(salt称为盐,是一串随机字符串,如果salt不一样则加密出来的结果也是不一样的)
由于salt是一个随机数因此下面两个按钮中执行的结果也是不一样的!
废话少说,还是给出相应的代码:
#region ---生成加密盐
public string CreateSalt(int size)
{
//使用加密服务提供程序 (CSP) 提供的实现来实现加密随机数生成器 (RNG).
//using System.Security.Cryptography;
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
//返回Base64字符串表示形式的随机数
return Convert.ToBase64String(buff);
}
#endregion
#region ---SHA1+Salt进行加密
private string createPwdHashSHA1(string pwd, string salt)
{
string saltAndPwd = string.Concat(pwd, salt);
string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "SHA1");
return hashedPwd;
}
#endregion
#region ---MD5+Salt进行加密
private string createPwdHashMD5(string pwd, string salt)
{
string saltAndPwd = string.Concat(pwd, salt);
string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "MD5");
return hashedPwd;
}
#endregion
#region ---进行加密
protected void Button1_Click(object sender, EventArgs e)
{
string salt = this.CreateSalt(32);
Label7.Text = this.createPwdHashSHA1(TextBox1.Text, salt);//salt+sha1
Label6.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "SHA1");//sha1
Label8.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");//md5
Label9.Text = this.createPwdHashMD5(TextBox1.Text, salt);//md5+salt
}
#endregion
protected void Button2_Click(object sender, EventArgs e)
{
string salt = this.CreateSalt(32);
Label11.Text = this.createPwdHashSHA1(TextBox1.Text, salt);//salt+sha1
Label10.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "SHA1");//sha1
Label12.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");//md5
Label13.Text = this.createPwdHashMD5(TextBox1.Text, salt);//md5+salt
}
相关文章推荐
- 利用ASP.NET加密和解密Web.config中连接字符串
- (译)利用ASP.NET加密和解密Web.config中连接字符串
- VS2010下asp.net 对现有的PDF文档进行加密(利用iTextSharp)
- 利用ASP.NET加密和解密Web.config中连接字符串
- 利用ASP.NET加密和解密Web.config中连接字符串
- 利用ajax.dll进行asp.net ajax开发
- asp.net用UrlEncode怎么写代码来实现对IE地址栏的地址进行编码"加密"
- asp.net 利用多表联合查询进行汇总统计
- java 利用HashMap对字符串进行加密和解密
- 利用客户端缓存对网站进行优化 - asp.net客户端缓存
- 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
- 利用Filter对asp.net页面进行压缩
- [英文]利用Visual Studio.NET 进行 ASP.NET的WEB开发
- 在asp.net中使用加密数据库联接字符串保证数据安全
- ASP.NET数据库连接字符串的加密与解密
- 利用WWF进行ASP.NET程序开发
- 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
- ASP.NET 加密 Part.5(加密查询字符串)