473,408 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 30448
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: OldGuy | last post by:
Hi All Sendmail 8.12.11 php 4.3.9 Sendmail is installed and works properly. php is NOT in safemode from the command line; mail user@domain.com < testmsg works fine.
0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
2
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's userid and passed with RSACryptoServiceProvider....
1
by: Ron Holmes | last post by:
I posted this question on the Crystal Reports Support site and I am still waiting for an answer. Using Crystal Reports 9.0 Developer Full edition: My Crystal report .RPT file has a Picture box...
0
by: Özden Irmak | last post by:
Hello, In my application, I've to cast DocumentDesigner class to my own derived class,MyDocumentDesigner, in order to reach the protected properties in DocumentDesigner class. But everytime I...
3
by: Michael Conroy | last post by:
Hi... Synposis... Throws exception: "Specified argument was out of the range of valid values." Read on for the juicy tidbits. MySimpleClassCol mscc=new MySimpleClassCol(); private void...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
2
by: user | last post by:
Hi everyone, Im using the following conn string in asp.net 1.1 using vb.net: Dim conn As New OleDbConnection("Provider=MS Remote;Remote Server=http://myintranetserver/; Remote...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.