【问题标题】:Decode using available method for encoding in VB使用 VB 中可用的编码方法进行解码
【发布时间】:2014-09-15 22:50:22
【问题描述】:

我有这个方法

Public Shared Function HashPassword(ByVal password As String) As String

        Dim algorithm As HashAlgorithm
        algorithm = SHA1.Create
        Dim data As Byte() = algorithm.ComputeHash(Encoding.UTF8.GetBytes(password))
        Dim HashedPassword As String = ""
        Dim i As Integer = 0
        Do While i < data.Length
            HashedPassword &= data(i).ToString("x2").ToLowerInvariant()
            i += 1
        Loop
        Return HashedPassword

    End Function

如何解码已被此函数编码的字符串?

【问题讨论】:

  • Unhashing a hash C# 的可能重复项
  • 通常将结果(data)转换为 Base64:hash = Convert.ToBase64String(data) 不确定你在那里做什么会起作用
  • 你不能。这就是Hash 的全部意义所在。

标签: vb.net decode encode


【解决方案1】:

哈希是一种方法,因此您无需解密它们。相反,将新输入值与存储的哈希值进行比较以进行验证。如果您想加密/解密数据(不是保护密码的最佳方式),那么您需要加密,而不是散列。

但您的方法可能存在问题:结果通常转换为 base64 用于存储/保存。十六进制字符串可能有效(从未尝试过),但有一种内置方法:

Public Shared Function HashPassword(password As String) As String

    Dim algorithm As HashAlgorithm
    algorithm = SHA1.Create
    Dim data As Byte() = algorithm.ComputeHash(Encoding.UTF8.GetBytes(password))

    Return Convert.ToBase64String(data)

End Function

因此,新用户注册将类似于:

hashedPW = HashPassword(thePWText)

稍后检查:

thisPW = HashPassword(PWInput)

If thisPW = hashedPW Then
   ' user knows the PW
Else
   ' wrong password
End If

【讨论】:

    猜你喜欢
    • 2017-10-26
    • 2011-04-03
    • 1970-01-01
    • 2014-01-24
    • 2015-07-23
    • 2018-08-14
    • 2011-01-30
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多