如何解密加密HMACSHA1一个字符串? [英] How to decrypt a string encrypted with HMACSHA1?
问题描述
我是一个加密的新手试图通过一些值来回系统之间。我可以加密的价值,但似乎无法弄清楚如何解密的另一端。我已经使用VB.NET创建了一个简单的Windows窗体应用程序。试图输入一个值和密钥,加密,然后解密得到原始值。这是我的code为止。任何帮助非常AP preciated。谢谢你。
导入系统
进口System.IO
进口System.Security.Cryptography
进口System.Text
公共类Form1中
私人小组btnEn code_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)处理btnEn code.Click
昏暗HMACSHA1作为新HMACSHA1(Encoding.ASCII.GetBytes(txtKey.Text))
昏暗的散列值作为字节()= hmacsha1.ComputeHash(Encoding.ASCII.GetBytes(txtValue.Text))
txtResult.Text = BytesToHexString(散列值)
hmacsha1.Clear()
结束小组
私人小组btnDe code_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)处理btnDe code.Click
???
结束小组
专用功能BytesToHexString(BYVAL字节字节())作为字符串
昏暗的输出作为字符串=的String.Empty
昏暗我作为整数= 0
做,而I< bytes.Length
输出+ =字节(ⅰ)的ToString(X2)
I + = 1
循环
返回输出
端功能
末级
HMAC-SHA1是单向哈希 ,而不是一个双向加密算法。你不能解密。我没有时间在这里提供完整的加密code - 这是一个复杂的话题,但巴里多兰斯'开始ASP .NET安全会给你一个很好的起点。 (只有一部分是ASP.NET特有的。)您也可以看他的 DDD谈的话题。
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屋!