472,780 Members | 4,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 30306
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: ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.