Question

Here 's my code and I have no idea why it produces a weird code in the console

(output is "b5?2???p?????'5???.?H???Kun???a??\??d??+\??%??A)?_???j?" without the quotes)

Private Sub TestSHA512()

    Dim key As String = "635357773463315343"
    Dim pass As String = "somepasswd"

    Dim enc As System.Text.Encoding = New System.Text.ASCIIEncoding

    Dim keyBytes() As Byte = enc.GetBytes(key)
    Dim passBytes() As Byte = enc.GetBytes(pass)

    Dim SHA As New HMACSHA512(keyBytes)

    Dim resultBytes() As Byte = SHA.ComputeHash(passBytes)

    Console.WriteLine(enc.GetString(resultBytes))
    Console.WriteLine(enc.GetString(SHA.Hash)) 'same...

End Sub
Était-ce utile?

La solution

First, SHA512 is a hash algorithm, not an encryption scheme, so if you're trying to encrypt then SHA512 isn't the way to do it. You'd need to look at an encryption class, such as AesManaged.

ComputeHash gives you the computed hash as a byte array. You're using ASCIIEncoding.GetString to convert that into a string, but not every byte is a printable ASCII character. That's why you're seeing the ??? characters in your console output.

If you're asking how to display the hash output as a printable string, use Convert.ToBase64String, which will convert the byte array into a string using base64 encoding. If you were expecting it in hexadecimal, you can loop through the byte array and print the Hex() value of each byte.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top