.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=に暗号化されます
私は行き詰まっています。ここからどこへ行けばいいのかわからない。どんな助けでも大歓迎です!