如何解密使用HMACSHA1加密的字符串? [英] How to decrypt a string encrypted with HMACSHA1?

查看:340
本文介绍了如何解密使用HMACSHA1加密的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个加密新手,试图在系统之间来回传递一些值。我可以加密的值,但似乎无法弄清楚如何解密在另一端。我使用VB.NET创建了一个简单的Windows Forms应用程序。尝试输入一个值和一个密钥,加密然后解密以获得原始值。这是我的代码到目前为止。任何帮助非常感谢。谢谢。

 导入系统
导入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆