ASP.NET中散列加密密码

private void LoginButton_Click(object sender,System.EventArgs e)

{

String sql=String.Format("select password from Administrator where AdminID='{0}',UseridBox.Text);

SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);

SqlDataReader myreader=new SqlCommand(sql,conn).ExecuteReader();

if(myreader.Read())

{

String hashed=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordTextBox.Text,"SHA1");

//将用户输入的密码哈希后再与数据库是的哈希值进行比较

if(hash==myreader["password"]).ToString())

{

FormsAuthentication.RedirectFromLoginPage(UseridBox.Text,true);//转到请求页

}

else

Result.Text="密码错误";

}

else

Result.Text="用户不存在";

conn.Close();

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC环境,可以使用System.Security.Cryptography命名空间的类来实现加密和解密。 例如,可以使用SymmetricAlgorithm类来加密和解密数据。以下是一个示例代码: ``` public static string Encrypt(string plainText, string key) { byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); byte[] keyBytes = Encoding.UTF8.GetBytes(key); SymmetricAlgorithm algorithm = Aes.Create(); algorithm.Key = keyBytes; MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(plainBytes, 0, plainBytes.Length); cryptoStream.FlushFinalBlock(); byte[] encryptedBytes = memoryStream.ToArray(); return Convert.ToBase64String(encryptedBytes); } public static string Decrypt(string encryptedText, string key) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); byte[] keyBytes = Encoding.UTF8.GetBytes(key); SymmetricAlgorithm algorithm = Aes.Create(); algorithm.Key = keyBytes; MemoryStream memoryStream = new MemoryStream(encryptedBytes); CryptoStream cryptoStream = new CryptoStream(memoryStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read); byte[] plainBytes = new byte[encryptedBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length); return Encoding.UTF8.GetString(plainBytes, 0, decryptedByteCount); } ``` 在此示例,Encrypt方法接受明文和密钥作为输入,并返回加密后的文本。Decrypt方法接受加密后的文本和密钥作为输入,并返回解密后的明文。 请注意,在实际使用,需要注意密钥的安全性和存储方式。密钥的安全性对加密的强度有很大的影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值