468,289 Members | 1,778 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

TripleDES "Specified key not a valid size for this algorithm"

GRB
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
Nov 23 '05 #1
2 29810
For Single DES the key is fixed size of 8 bytes. For Triple DES it is 24.
The DES algorithm does not allow for any variation in the key length. This
is why you are getting the error. It may be possible that the 44 character
key contains a 24 byte key in some kind of key transport envelope. (possibly
signed??) Whatever it is it, it cannot be a valid TripleDES key.

"GRB" wrote:
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

Nov 23 '05 #2
GRB
Thanks Is there a .net algorithm the I can use for DESede?

"TrtnJohn" wrote:
For Single DES the key is fixed size of 8 bytes. For Triple DES it is 24.
The DES algorithm does not allow for any variation in the key length. This
is why you are getting the error. It may be possible that the 44 character
key contains a 24 byte key in some kind of key transport envelope. (possibly
signed??) Whatever it is it, it cannot be a valid TripleDES key.

"GRB" wrote:
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

Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Özden Irmak | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.