UTF8编码的密码Byte[]用SHA512加密到字符串转换

发布于 2024-11-27 15:45:17 字数 776 浏览 1 评论 0原文

我用 C# 创建了一个 Web 表单,它接受用户名和密码,并将密码以“图像”格式存储在 MSSQL 2005 数据库中。密码与盐合并,以 UTF8 编码,最后应用 SHA512 加密。当我从数据库中提取密码时,我希望能够看到字符串格式的密码。如果以下是我加密密码的方式,我的解密函数应该如何?这可能吗? :

    string loginID = "";//This will be stored in varchar format in MSSQL..(Unrelated to the question)
    string password =""; //This is where I store password inputted by user.
    Random r = new Random();
    int salt = r.Next((int)Math.Pow(2, 16));
    int verifyCode = r.Next((int)Math.Pow(2, 16));
    string tmpPwd = password.ToLower() + salt.ToString();
    UTF8Encoding textConverter = new UTF8Encoding();
    byte[] passBytes = textConverter.GetBytes(tmpPwd);
    byte[] hashedPWD = new SHA512Managed().ComputeHash(passBytes);

hashedPWD 中的值作为图像数据类型存储在 MSSQL 中,盐作为 int 存储。

I have created a web form in c# that accepts username and password and stores password in MSSQL 2005 db in 'image' format. The password is merged with salt, encoded in UTF8 and lastly it is applied with a SHA512 encryption. I want to be able to see the passwords in string format when I pull them up back from the database. How should my decrypt function be, if the following is how I encrypted the password? Is that possible? :

    string loginID = "";//This will be stored in varchar format in MSSQL..(Unrelated to the question)
    string password =""; //This is where I store password inputted by user.
    Random r = new Random();
    int salt = r.Next((int)Math.Pow(2, 16));
    int verifyCode = r.Next((int)Math.Pow(2, 16));
    string tmpPwd = password.ToLower() + salt.ToString();
    UTF8Encoding textConverter = new UTF8Encoding();
    byte[] passBytes = textConverter.GetBytes(tmpPwd);
    byte[] hashedPWD = new SHA512Managed().ComputeHash(passBytes);

The value in hashedPWD is stored in MSSQL as image datatype and salt is stored as int.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-12-04 15:45:17

你不能——根据定义,哈希函数就是一种单向函数。直到最后一行,您都可以取回密码,但是在哈希函数之后,您所能做的就是生成第二个哈希并比较两者以查看它们是否产生相同的结果,在这种情况下您可以假设源字符串是相同的。

You can't - that's what a hash function is, by definition - a one-way function. Up until the last line, you can get the password back, but after the hash function, all you can do is generate a second hash and compare the two to see if they've produced the same result, in which case you can presume that the source strings were the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文