2

我无法解密在 .NET 中加密的字符串。我在这里看到了其他几个解决方案,主要是在 C# 中解密用 3DES 加密的 ColdFusion 中的字符串,但我在尝试解密传递给我的 .NET 加密字符串时仍然遇到问题。这是错误:

“尝试加密或解密您的输入字符串时发生错误:给定最终块未正确填充。”

在进一步的测试中,当开始使用相同的解密文本时,我在 CF 中得到的加密字符串与在 .NET 中不同

这是用于加密/解密的 .NET 代码 - 我用填写信息替换了密钥和 IV 数据:

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class TripleDES

    ' define the triple des provider
    Private Shared m_des As New TripleDESCryptoServiceProvider

    ' define the string handler
    Private m_utf8 As New UTF8Encoding

    ' define the local property arrays
    Private m_key() As Byte
    Private m_iv() As Byte
    Private key As String = "48-digit hex key"
    Private iv As String = "16-digit hex iv"

    Public Sub New()
        Me.m_key = convertHexStringToByteArray(key)
        Me.m_iv = convertHexStringToByteArray(iv)
    End Sub
    Public Sub New(ByVal _key As String, ByVal _iv As String)
        key = _key
        iv = _iv
        Me.m_key = convertHexStringToByteArray(key)
        Me.m_iv = convertHexStringToByteArray(iv)
    End Sub
    Public Sub New(ByVal _key() As Byte, ByVal _iv() As Byte)
        Me.m_key = _key
        Me.m_iv = _iv
    End Sub
    Public Function Encryptfrombyte(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
    End Function

    Public Function EncryptBytes(ByVal text As String) As Byte()
        Dim input() As Byte = m_utf8.GetBytes(text)
        Dim DesEnc As ICryptoTransform = m_des.CreateEncryptor(m_key, m_iv)
        Dim output() As Byte = Transform(input, DesEnc)
        Return output 'convertByteArrayToHexString(output)
    End Function

    Public Function encrypt(ByVal key As Byte(), ByVal data As Byte()) As Byte()
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = key
        des.Mode = CipherMode.ECB
        des.Padding = PaddingMode.PKCS7
        Return des.CreateEncryptor.TransformFinalBlock(data, 0, data.Length)
    End Function
    Public Function Decrypt(ByVal input() As Byte) As String
        Dim DesEnc As ICryptoTransform = m_des.CreateDecryptor(m_key, m_iv)
        Dim output() As Byte = Transform(input, DesEnc)

        Return m_utf8.GetString(output)
    End Function
    Public Function Decrypt(ByVal text As String) As String
        Dim input() As Byte = m_utf8.GetBytes(text)
        Dim output() As Byte = Transform(input, _
                         m_des.CreateDecryptor(m_key, m_iv))
        Return m_utf8.GetString(output)
    End Function

    Private Function Transform(ByVal input() As Byte, _
        ByVal CryptoTransform As ICryptoTransform) As Byte()
        ' create the necessary streams
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New _
            CryptoStream(memStream, CryptoTransform, _
            CryptoStreamMode.Write)
        ' transform the bytes as requested
        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        ' Read the memory stream and convert it back into byte array
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        ' close and release the streams
        memStream.Close()
        cryptStream.Close()
        ' hand back the encrypted buffer
        Return result
    End Function
    Public Function convertHexStringToByteArray(ByVal str As String) As Byte()

        '//String in Hex - 2 chars represet a byte.
        Dim size As Integer = (str.Length() / 2) - 1
        Dim b(size) As Byte
        'ReDim b  str.Length() / 2
        Dim j As Integer = 0

        Try
            For i As Integer = 0 To str.Length() - 1 Step 2
                Dim s As String = str.Substring(i, 2)
                '//parse the substring as an integer, as the 8th bit may be set
                Dim k As Integer = Convert.ToInt32(s, 16)
                '//downcast integer to byte, this down casting does not do any
                ' //harm since memory footprint of first 8 bits remains same.
                b(j) = New Byte
                b(j) = CType(k, Byte)
                j += 1
            Next
        Catch e As Exception
            Dim ti As String = 1
            'TraceLog.trace2(e)
        End Try
        Return b
    End Function
    Public Function convertByteArrayToHexString(ByVal b() As Byte) As String
        Dim buffer As String
        ' Dim hexDigit() As String = Split("0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f", "', '")

        For i As Integer = 0 To b.Length - 1
            'dim  array as char()= (hexDigit((b(i) >> 4) & chr(0x0f)), hexDigit(b(i) & 0x0f))
            Dim tmp As String = Hex(b(i))
            If tmp.Length = 1 Then
                tmp = "0" & tmp
            End If
            buffer += (tmp)
        Next

        Return buffer.ToString()
    End Function
    Public Function bytetoHex(ByVal hextBt() As Byte) As String
        Dim disc As Integer = 0
        bytetoHex = HexEncoding.ToString(hextBt)

    End Function

    Public Function hexToByte(ByVal hex As String) As Byte()
        Dim disc As Integer = 0
        hexToByte = HexEncoding.hexidecimaltoByte(hex)
    End Function

End Class

这是加密/解密的 CF 代码:

<!--- Get The Key from Hex to base64 --->
<cfset theKeyHex    = "48-digit hex key" />
<cfset theKeyBin    = BinaryDecode(theKeyHex,"hex")>
<cfset theKeyB64    = BinaryEncode(theKeyBin,"base64")>

<!--- Get the IV to Binary --->
<cfset theIV        = "16-digit hex iv" />
<cfset theIVBin     = BinaryDecode(theIV,"hex")>

<!--- Define Encoding parameters --->
<cfset theAlgorithm = "DESede/ECB/PKCS5Padding">
<cfset theEncoding  = "Base64">

<!--- Define string --->
<cfset str = "108644" >

<cfoutput>
    <cfset enc = Encrypt(str, theKeyB64, theAlgorithm, theEncoding, theIVBin)>
    encrypted = #enc#<br />

    <cfset dec = Decrypt(enc, theKeyB64, theAlgorithm, theEncoding, theIVBin)>
    decrypted = #dec#<br />
</cfoutput>

为了清楚起见 - 在尝试测试此代码时,我在解密 .NET 加密字符串时遇到了错误,因此我尝试加密 CF 中的值以查看是否得到相同的加密字符串。

在 CF 中,108644 被加密为 63Yp6O+8K+U=

在 .NET 中,108644 被加密为 7loa00RCdZo=

我陷入了僵局。我不知道从这里去哪里。任何帮助是极大的赞赏!

4

1 回答 1

2

ECBmode 不使用初始化向量,因此被忽略。您需要将模式更改为CBC

<cfset theAlgorithm = "DESede/CBC/PKCS5Padding">

编辑:详细说明,模式因.NET 代码使用的功能而异。该encrypt(key, data)函数使用ECB模式。虽然EncryptBytes(text)隐式使用CBC模式,但由于存在iv. 因此,您需要相应地更改您的 CF 代码。

于 2012-05-14T17:04:38.673 回答