【问题标题】:Not the excpected hash from the scrypt algo不是来自 scrypt 算法的预期哈希
【发布时间】:2022-01-22 03:33:32
【问题描述】:

我在我的 vb 应用程序中使用来自 here 的这个 scrypt

我试过这段代码来散列一个十六进制字符串:

Imports Replicon.Cryptography.SCrypt

Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim ss() As Byte = System.Text.Encoding.Default.GetBytes(TextBox1.Text)

        RichTextBox1.Text = System.Text.Encoding.Default.GetString(SCrypt.DeriveKey(ss, ss, 1024, 1, 1, 32))


    End Sub
End Class

文本框内的十六进制字符串:

TextBox1.text = "01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f279fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4ea78e011d012d59d4"

它给了我这个:

r3Î<ÛãhšÏ-$:8´"ýäP+°‡ W«&‰Â

小端反转后的预期结果是这样的:

0000000110c8357966576df46f3b802ca897deb7ad18b12f1c24ecff6386ebd9

我觉得问题是字节转换,字符串转换?

感谢您的帮助

谢谢大家。

【问题讨论】:

  • SCrypt.DeriveKey() 的输出是一个随机字节序列。它们不是字符串的编码,因此将它们解码为字符串没有任何意义。将它们保留为字节并没有错,但是如果您必须有一个字符串,则使用 base64 或十六进制编码器对字节进行编码。再次使用它们时,您必须对其进行解码。
  • 谢谢你更新我的问题

标签: vb.net hash hex scrypt litecoin


【解决方案1】:

我找到了解决办法:

代码是:


Imports Replicon.Cryptography.SCrypt
Imports System.Globalization
Imports System.Text

Public Class Form1


    Public Shared Function Btog(ByVal ba() As Byte) As String
        Dim hex As StringBuilder = New StringBuilder(ba.Length * 2)
        Dim b As Byte
        For Each b In ba
            hex.AppendFormat("{0:x2}", b)
        Next
        Return hex.ToString()
    End Function



    Public Function HexDecode(ByVal s As String) As Byte()
        Return HexDecode(s, 0)
    End Function

    Public Function HexDecode(ByVal s As String, ByVal paddingBytes As Integer) As Byte()
        If s Is Nothing Then
            Throw New ArgumentNullException("s")
        End If

        If s.IndexOf(":"c) > -1 Then
            s = s.Replace(":", "")
        End If

        If (s.Length Mod 2) <> 0 Then
            Throw New FormatException("parameter 's' must have an even number of hex characters")
        End If

        Dim result As Byte() = New Byte(s.Length \ 2 + (paddingBytes - 1)) {}
        For i As Integer = 0 To result.Length - paddingBytes - 1
            result(i) = Byte.Parse(s.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier)
        Next
        Return result
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click



        RichTextBox1.Text = Btog(SCrypt.DeriveKey(HexDecode(TextBox1.Text), HexDecode(TextBox1.Text), 1024, 1, 1, 32))


    End Sub
End Class


【讨论】:

    猜你喜欢
    • 2012-07-19
    • 2016-11-16
    • 1970-01-01
    • 2021-07-23
    • 2012-07-31
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多