G
Guest
A client wants me to decrypt a cookie. They encrypted the data in java using
TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm.
Ive tried to decrypt in vb.net and get the above error. Below is the class I
use and the code I use to call it.
Not sure wher I'm going wrong here. The TripleDES service provider only uses
a 192 bit 24 character length key.
Any help would be greatly appreciated.
DIM sCipherKey as String = "12345678901234567890123456789012345678901234"
Dim key() As Byte = StrToByteArray(sCipherKey)
Dim iv() As Byte = {12, 34, 56, 78, 90, 87, 65, 43}
Dim des As New cTripleDES(key, iv)
Dim decryptedData As String = "12345|1|2005-10-05 11:21:31.128"
Dim newEncryptedData As String = des.Encrypt(decryptedData)
decryptedData = des.Decrypt(newEncryptedData )
Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.UTF8Encoding
Return encoding.GetBytes(str)
End Function 'StrToByteArray
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Friend Class cTripleDES
' define the triple des provider
Private 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
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
End Function
Public Function Decrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
End Function
Public Function Encrypt(ByVal text As String) As String
Dim input() As Byte = m_utf8.GetBytes(text)
Dim output() As Byte = Transform(input, m_des.CreateEncryptor(m_key,
m_iv))
Return Convert.ToBase64String(output)
End Function
Public Function Decrypt(ByVal text As String) As String
Dim input() As Byte = Convert.FromBase64String(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
End Class
TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm.
Ive tried to decrypt in vb.net and get the above error. Below is the class I
use and the code I use to call it.
Not sure wher I'm going wrong here. The TripleDES service provider only uses
a 192 bit 24 character length key.
Any help would be greatly appreciated.
DIM sCipherKey as String = "12345678901234567890123456789012345678901234"
Dim key() As Byte = StrToByteArray(sCipherKey)
Dim iv() As Byte = {12, 34, 56, 78, 90, 87, 65, 43}
Dim des As New cTripleDES(key, iv)
Dim decryptedData As String = "12345|1|2005-10-05 11:21:31.128"
Dim newEncryptedData As String = des.Encrypt(decryptedData)
decryptedData = des.Decrypt(newEncryptedData )
Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.UTF8Encoding
Return encoding.GetBytes(str)
End Function 'StrToByteArray
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Friend Class cTripleDES
' define the triple des provider
Private 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
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
End Function
Public Function Decrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
End Function
Public Function Encrypt(ByVal text As String) As String
Dim input() As Byte = m_utf8.GetBytes(text)
Dim output() As Byte = Transform(input, m_des.CreateEncryptor(m_key,
m_iv))
Return Convert.ToBase64String(output)
End Function
Public Function Decrypt(ByVal text As String) As String
Dim input() As Byte = Convert.FromBase64String(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
End Class