Post by eichmat on Mar 1, 2007 16:57:07 GMT -5
In an old VB6 project, I have to implement a process that pulls an encrypted value from a data table. We have a .NET project from which I've mimicked the same logic using VBCorLib v2.1.
Everything works great except, whenever I call FlushFinalBlock, CloseStream or attempt to destroy the NewCryptoStream object, I get an error from VBCorLib:
Below is the code.
Any help (ASAP) would be very greatfully appreciated!
-Tim
Everything works great except, whenever I call FlushFinalBlock, CloseStream or attempt to destroy the NewCryptoStream object, I get an error from VBCorLib:
Run-time error '-2146233296 (80131430)':
Invalid data length.
Below is the code.
Any help (ASAP) would be very greatfully appreciated!
-Tim
Private Function DecryptValue(ByVal EncyrptedValue As String, ByVal Key As String, ByVal IV As String) As String
Dim bytDecrypted() As Byte
Dim bytEncrypted() As Byte
Dim bytIV() As Byte
Dim bytKey() As Byte
Dim strValue As String
Dim objDecryptor As RijndaelManaged
Dim objMD5 As MD5CryptoServiceProvider
Dim intCounter As Integer
Dim lngLength As Long
Dim objMemoryStream As MemoryStream
Dim objCryptStream As CryptoStream
Dim sbdData As StringBuilder
strValue = vbNullString
bytKey() = StrConv(Key, vbFromUnicode)
bytIV() = StrConv(IV, vbFromUnicode)
Set objDecryptor = New RijndaelManaged
With objDecryptor
Set objMD5 = New MD5CryptoServiceProvider
.Key = objMD5.ComputeHash(bytKey())
.IV = objMD5.ComputeHash(bytIV())
Set objMD5 = Nothing
.Mode = CipherMode.CBC
End With
'Create the crypto objects
Set objMemoryStream = New MemoryStream
Set objCryptStream = NewCryptoStream(objMemoryStream, objDecryptor.CreateDecryptor(), CryptoStreamMode.WriteMode)
Set objDecryptor = Nothing
bytEncrypted = StrConv(EncyrptedValue, vbFromUnicode)
objCryptStream.WriteBlock bytEncrypted, 0, UBound(bytEncrypted)
objCryptStream.FlushFinalBlock
Set objCryptStream = Nothing
Set sbdData = New StringBuilder
bytDecrypted() = objMemoryStream.ToArray
Set objMemoryStream = Nothing
For intCounter = 0 To UBound(bytDecrypted)
sbdData.Append Chr(bytDecrypted(intCounter))
Next
strValue = sbdData.ToString()
DecryptValue = Trim(strValue)
End Function