I'm an encryption novice trying to pass some values back and forth between systems. I can encrypt the value, but can't seem to figure out how to decrypt on the other end. I've created a simple Windows Forms application using VB.NET. Trying to input a value and a key, encrypt and then decrypt to get the original value. Here's my code so far. Any help greatly appreciated. Thanks.
Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
Dim hmacsha1 As New HMACSHA1(Encoding.ASCII.GetBytes(txtKey.Text))
Dim hashValue As Byte() = hmacsha1.ComputeHash(Encoding.ASCII.GetBytes(txtValue.Text))
txtResult.Text = BytesToHexString(hashValue)
hmacsha1.Clear()
End Sub
Private Sub btnDecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecode.Click
'???
End Sub
Private Function BytesToHexString(ByVal bytes As Byte()) As String
Dim output As String = String.Empty
Dim i As Integer = 0
Do While i < bytes.Length
output += bytes(i).ToString("X2")
i += 1
Loop
Return output
End Function
End Class
HMAC-SHA1 is a one-way hash, not a bidirectional encryption algorithm. You can't decrypt it. I don't have time to provide full encryption code here - it's a complicated topic, but Barry Dorrans' "Beginning ASP.NET Security" would give you a good starting point. (Only some of it is ASP.NET-specific.) You could also watch his DDD talk on the topic.