I need help porting a function from vb.net to vb 6.0 the function is TripleDESDecode this is a very common function using the TripleDESCryptoServiceProvider which is with in this VBCorLib Class.
Heres the original code from vb.net:
Private Function TripleDESDecode(ByVal value As String, ByVal key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = New Byte(8 - 1) {}
provider.Key = New PasswordDeriveBytes(key, New Byte(0 - 1) {}).CryptDeriveKey("RC2", "MD5", &H80, New Byte(8 - 1) {})
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream(value.Length)
Dim stream As New CryptoStream(stream2, provider.CreateDecryptor, CryptoStreamMode.Write)
stream.Write(buffer, 0, buffer.Length)
stream.FlushFinalBlock
Dim buffer2 As Byte() = New Byte((CInt((stream2.Length - 1)) + 1) - 1) {}
stream2.Position = 0
stream2.Read(buffer2, 0, CInt(stream2.Length))
stream.Close
Return Encoding.UTF8.GetString(buffer2)
End Function
Now heres what I have come up with so far in VB 6.0:
Function TripleDESDecode(ByVal value As String, ByVal Key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = NewBytes(8 - 1) 'ERROR HERE
provider.Key = New PasswordDeriveBytes(key, NewBytes(0 - 1)).CryptDeriveKey("RC2", "MD5", &H80, NewBytes(8 - 1)) 'ERROR HERE! - CAN'T FIND ALTERNATION FUNCTIONS!
Dim buffer() As Byte
buffer = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream
stream2 = NewMemoryStream(NewBytes(Len(value))) 'ERROR HERE!
'value.Length
Dim stream As CryptoStream
Set stream = NewCryptoStream(stream2, provider.CreateDecryptor, WriteMode)
stream2, provider.CreateDecryptor, CryptoStreamMode.Write
stream.WriteBlock buffer, 0, VBCorLib.buffer.ByteLength(buffer) 'ERROR HERE!
stream.FlushFinalBlock ' ERROR HERE
Dim buffer2() As Byte
buffer2 = NewBytes((CInt(stream2.Length - 1) + 1))
stream2.Position = 0
stream2.ReadBlock buffer2, 0, CInt(stream2.Length)
stream.CloseStream 'ERROR HERE!
TripleDESDecode = Encoding.UTF8.GetString(buffer2)
End Function
If someone can give me a hand converting this it would be a great help!
Heres an example of the data passed to the function.
value = "D95LUtQqHIo="
key = "OTJWNaYmlg5J6n1gpakwn2zT75OkR8DH60S1VI2uTOp6zVdhXcsEwiH0bp8S9d45y30f1QiCCXXzADMQ2KB5Er6dcxeAiemqGlbNf9Q442FbI479h7354ZyLLGg3jMvY"
Thanks,
J Greene
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just so you know, VBCorLib doesn't implement the PasswordDeriveBytes class, only the Rfc2898DeriveBytes class for generating keys from passwords.
Secondly, the NewBytes function doesn't create a new empty byte array of a specified length. It is a method that converts a series of values into a byte array. You can use the cArray.CreateInstance method to create an arbitrary size empty array of any type, or declare a local variable and ReDim it to the desired length.
Thirdly, you need to use 'Set' with the NewMemoryStream function.
All of the Stream errors are probably due to the stream2 object not being properly initialized.
Barring the lack of the PasswordDeriveBytes class, I would implement the rest of the method something like that:
Function TripleDESDecode(ByVal value As String, ByVal Key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = cArray.CreateInstance(ciByte, 8)
' This is the replacement of the PasswordDerivedBytes portion. This won't
' be compatible with your version, however.
Dim pwd As Rfc2898DeriveBytes
Set pwd = Cor.NewRfc2898DeriveBytes(Key, NewBytes(0, 0, 0, 0, 0, 0, 0, 0))
provider.Key = pwd.GetBytes(16)
Dim buffer() As Byte
buffer = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream
Dim stream As CryptoStream
Set stream = NewCryptoStream(stream2, provider.CreateDecryptor, WriteMode)
Welcome to Help
I need help porting a function from vb.net to vb 6.0 the function is TripleDESDecode this is a very common function using the TripleDESCryptoServiceProvider which is with in this VBCorLib Class.
Heres the original code from vb.net:
Private Function TripleDESDecode(ByVal value As String, ByVal key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = New Byte(8 - 1) {}
provider.Key = New PasswordDeriveBytes(key, New Byte(0 - 1) {}).CryptDeriveKey("RC2", "MD5", &H80, New Byte(8 - 1) {})
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream(value.Length)
Dim stream As New CryptoStream(stream2, provider.CreateDecryptor, CryptoStreamMode.Write)
stream.Write(buffer, 0, buffer.Length)
stream.FlushFinalBlock
Dim buffer2 As Byte() = New Byte((CInt((stream2.Length - 1)) + 1) - 1) {}
stream2.Position = 0
stream2.Read(buffer2, 0, CInt(stream2.Length))
stream.Close
Return Encoding.UTF8.GetString(buffer2)
End Function
Now heres what I have come up with so far in VB 6.0:
Function TripleDESDecode(ByVal value As String, ByVal Key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = NewBytes(8 - 1) 'ERROR HERE
provider.Key = New PasswordDeriveBytes(key, NewBytes(0 - 1)).CryptDeriveKey("RC2", "MD5", &H80, NewBytes(8 - 1)) 'ERROR HERE! - CAN'T FIND ALTERNATION FUNCTIONS!
Dim buffer() As Byte
buffer = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream
stream2 = NewMemoryStream(NewBytes(Len(value))) 'ERROR HERE!
'value.Length
Dim stream As CryptoStream
Set stream = NewCryptoStream(stream2, provider.CreateDecryptor, WriteMode)
stream2, provider.CreateDecryptor, CryptoStreamMode.Write
stream.WriteBlock buffer, 0, VBCorLib.buffer.ByteLength(buffer) 'ERROR HERE!
stream.FlushFinalBlock ' ERROR HERE
Dim buffer2() As Byte
buffer2 = NewBytes((CInt(stream2.Length - 1) + 1))
stream2.Position = 0
stream2.ReadBlock buffer2, 0, CInt(stream2.Length)
stream.CloseStream 'ERROR HERE!
TripleDESDecode = Encoding.UTF8.GetString(buffer2)
End Function
If someone can give me a hand converting this it would be a great help!
Heres an example of the data passed to the function.
value = "D95LUtQqHIo="
key = "OTJWNaYmlg5J6n1gpakwn2zT75OkR8DH60S1VI2uTOp6zVdhXcsEwiH0bp8S9d45y30f1QiCCXXzADMQ2KB5Er6dcxeAiemqGlbNf9Q442FbI479h7354ZyLLGg3jMvY"
Thanks,
J Greene
Hello,
Just so you know, VBCorLib doesn't implement the PasswordDeriveBytes class, only the Rfc2898DeriveBytes class for generating keys from passwords.
Secondly, the NewBytes function doesn't create a new empty byte array of a specified length. It is a method that converts a series of values into a byte array. You can use the cArray.CreateInstance method to create an arbitrary size empty array of any type, or declare a local variable and ReDim it to the desired length.
Thirdly, you need to use 'Set' with the NewMemoryStream function.
All of the Stream errors are probably due to the stream2 object not being properly initialized.
Barring the lack of the PasswordDeriveBytes class, I would implement the rest of the method something like that:
Function TripleDESDecode(ByVal value As String, ByVal Key As String) As String
Dim provider As New TripleDESCryptoServiceProvider
provider.IV = cArray.CreateInstance(ciByte, 8)
' This is the replacement of the PasswordDerivedBytes portion. This won't
' be compatible with your version, however.
Dim pwd As Rfc2898DeriveBytes
Set pwd = Cor.NewRfc2898DeriveBytes(Key, NewBytes(0, 0, 0, 0, 0, 0, 0, 0))
provider.Key = pwd.GetBytes(16)
Dim buffer() As Byte
buffer = Convert.FromBase64String(value)
Dim stream2 As New MemoryStream
Dim stream As CryptoStream
Set stream = NewCryptoStream(stream2, provider.CreateDecryptor, WriteMode)
stream.WriteBlock buffer, 0, cArray.GetLength(buffer)
stream.FlushFinalBlock
stream.CloseStream
TripleDESDecode = Encoding.UTF8.GetString(stream2.ToArray)
End Function
Maybe this will help get you going in the right direction.
Kelly