如何解密使用HMACSHA1加密的字符串? [英] How to decrypt a string encrypted with HMACSHA1?
问题描述
导入系统
导入System.IO
导入System.Security.Cryptography
Imports System.Text
公共类Form1
Private Sub btnEncode_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnEncode.Click
Dim hmacsha1作为新的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)处理btnDecode.Click
'???
End Sub
私有函数BytesToHexString(ByVal bytes As Byte())As String
Dim输出As String = String.Empty
Dim i As Integer = 0
尽管我< bytes.Length
output + = bytes(i).ToString(X2)
i + = 1
循环
返回输出
结束函数
结束类
HMAC-SHA1是一种单向< href =http://en.wikipedia.org/wiki/Cryptographic_hash_function =noreferrer>哈希,而不是双向加密算法。你不能解密它。我没有时间在这里提供完整的加密代码 - 这是一个复杂的主题,但是Barry Dorrans的开始ASP.NET安全性将为您提供一个良好的起点。 (只有一些是ASP.NET特定的。)你也可以看他的 DDD talk 的话题。
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.
这篇关于如何解密使用HMACSHA1加密的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!