类型'1维字节数组'的值不能转换为字节 [英] Value of Type '1-dimensional array of byte' cannot be converted to Byte
本文介绍了类型'1维字节数组'的值不能转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我正在尝试将密码转换为哈希值以进一步保存,然后将其与登录进行匹配。但在编码时,上面的错误正在显示。这是我第一次进行散列。在此先感谢。
Hello,
I am trying to convert the password into hash value to further save and then match it for login. But while coding itself the above error is showing. This is my first time at hashing. Thanks in advance.
error is at line: Dim tmpSource As Byte = ASCIIEncoding.ASCII.GetBytes(sSourceData)
Imports System
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Module Hash
Public Function fnEncrypedValue(ByVal value)
Dim sSourceData As String = value
'Create a byte array from source data.
Dim tmpSource As Byte = ASCIIEncoding.ASCII.GetBytes(sSourceData)
'Compute hash based on source data.
Dim tmpHash As Byte = New SHA1CryptoServiceProvider().ComputeHash(tmpSource)
tmpHash = ByteArrayToString(tmpHash)
Return tmpHash
End Function
'Console.WriteLine(ByteArrayToString(tmpHash))
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim i As Integer
Dim sOutput As New StringBuilder(arrInput.Length)
For i = 0 To arrInput.Length - 1
sOutput.Append(arrInput(i).ToString("X2"))
Next
Return sOutput.ToString()
End Function
End Module
推荐答案
简单地说:
Simply:
Public Function fnEncrypedValue(ByVal value)
Dim sSourceData As String = value
'Create a byte array from source data.
Dim tmpSource As Byte() = ASCIIEncoding.ASCII.GetBytes(sSourceData)
'Compute hash based on source data.
Dim tmpHash As Byte() = New SHA1CryptoServiceProvider().ComputeHash(tmpSource)
tmpHash = ByteArrayToString(tmpHash)
Return tmpHash
End Function
GetBytes()
和 ComputeHash()
方法返回一个字节数组;你必须在你的变量声明中考虑到这一点。
GetBytes()
and ComputeHash()
methods return a byte array; you have to take that in account in your variables declarations.
这篇关于类型'1维字节数组'的值不能转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文