Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on - HMACSHA256 Encrypt

I am trying to encode a string with a sha256 and a password but I cant seem to get it right.. this is what I get instead.

Value of type 'String' cannot be converted to '1-dimensional array of Byte'.      

    Function HMACSHA256_Encrypt(ByVal Txt As String) As String
        HMACSHA256_Encrypt = Nothing
            Dim secretkey As String = "password"
            Dim sha As New System.Security.Cryptography.HMACSHA256(secretkey)
            Dim ae As New ASCIIEncoding()
            Dim Hash() As Byte = sha.ComputeHash(ae.GetBytes(Txt))
            Dim sb As New StringBuilder(Hash.Length * 2)
            Dim ndx As Integer

            For ndx = 0 To Hash.Length - 1
                sb.Append(Right("0" & Hex(Hash(ndx)), 2))
            Return sb.ToString.ToLower
        Catch ex As Exception
            Debug.Print(Date.Now & " SHA256_Encrypt error " & ex.Message)
        End Try
    End Function

Open in new window

Avatar of Pratima
Flag of India image

see this

Public Shared Function HashString(ByVal StringToHash As String) As String
    Dim myEncoder As New System.Text.UTF32Encoding
    Dim Key() As Byte = Convert.FromBase64String(My.Settings.PortalHASH)
    Dim XML() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
    Return Convert.ToBase64String(HashCode)
End Function

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER


beautiful, thank you so much!