473,325 Members | 2,608 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,325 software developers and data experts.

DES Decrypt Not Working

Hi folks,

My code (in VB.Net) will encrypt data fine (I guess...) but when I try
to decrypt it, it returns the exact same byte array that I passed to
*be* decrypted! Your advice would be most appreciated. My code is
pretty simple (too simple?)....

Function EncryptData(ByVal bData() As Byte) As Byte()
Dim eDES As New DESCryptoServiceProvider
Dim eMS As New MemoryStream(bData.Length)
Dim EncStr As New CryptoStream(eMS, _
eDES.CreateEncryptor(DESKey, DESiv),
CryptoStreamMode.Write)
EncStr.Write(bData, 0, bData.Length)
EncStr.FlushFinalBlock()
Dim bResult(eMS.Position) As Byte
eMS.Position = 0
eMS.Read(bResult, 0, bResult.Length)
EncStr.Close()
eMS.Close()
eDES.Clear()
Return bResult
End Function

Function DecryptData(ByVal bData() As Byte) As String
Dim DES As New DESCryptoServiceProvider
Dim MS As New MemoryStream(bData.Length)
Dim DecStr As New CryptoStream(MS, _
DES.CreateDecryptor(DESKey, DESiv),
CryptoStreamMode.Read)
MS.Write(bData, 0, bData.Length)
DecStr.FlushFinalBlock()
MS.Position = 0
Dim Ret As String = New StreamReader(MS).ReadToEnd
DecStr.Close()
MS.Close()
DES.Clear()
Return Ret
End Function
Jul 21 '05 #1
2 2516
JustMe <an********@hotmail.com> wrote:
My code (in VB.Net) will encrypt data fine (I guess...) but when I try
to decrypt it, it returns the exact same byte array that I passed to
*be* decrypted! Your advice would be most appreciated. My code is
pretty simple (too simple?)....

Function EncryptData(ByVal bData() As Byte) As Byte()
Dim eDES As New DESCryptoServiceProvider
Dim eMS As New MemoryStream(bData.Length)
Dim EncStr As New CryptoStream(eMS, _
eDES.CreateEncryptor(DESKey, DESiv),
CryptoStreamMode.Write)
EncStr.Write(bData, 0, bData.Length)
EncStr.FlushFinalBlock()
Dim bResult(eMS.Position) As Byte
eMS.Position = 0
eMS.Read(bResult, 0, bResult.Length)
EncStr.Close()
eMS.Close()
eDES.Clear()
Return bResult
End Function

Function DecryptData(ByVal bData() As Byte) As String
Dim DES As New DESCryptoServiceProvider
Dim MS As New MemoryStream(bData.Length)
Dim DecStr As New CryptoStream(MS, _
DES.CreateDecryptor(DESKey, DESiv),
CryptoStreamMode.Read)
MS.Write(bData, 0, bData.Length)
DecStr.FlushFinalBlock()
MS.Position = 0
Dim Ret As String = New StreamReader(MS).ReadToEnd
DecStr.Close()
MS.Close()
DES.Clear()
Return Ret
End Function


Well, you're relying on Stream.Read returning all the bytes you
requested in one chunk, which is in general unsafe but should be okay
with a MemoryStream. The MemoryStream.ToArray method makes it easier to
get the data in a MemoryStream, to be honest.

I note that you're decrypting to a string though, having *encrypted* a
byte array. This could well be part of the problem - was the data to be
encrypted a UTF-8 encoded version of a string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
I've sorted out the problem. This is what the code should look like:

** mMemStr is a global memorystream **

Function EncryptData(ByVal sData As String) As String
Dim eDES As New TripleDESCryptoServiceProvider
mMemStr = New MemoryStream
Dim EncStr As New CryptoStream(mMemStr, _
eDES.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
Dim mStrWri As New StreamWriter(EncStr)
mStrWri.Write(sData)
mStrWri.Flush()
EncStr.FlushFinalBlock()
Dim mBytes(mMemStr.Length - 1) As Byte
mMemStr.Position = 0
mMemStr.Read(mBytes, 0, mMemStr.Length)
Dim mEnc As New UTF8Encoding
Return = mEnc.GetString(mBytes)
End Function

Function DecryptData() As String
Dim DES As New TripleDESCryptoServiceProvider
mMemStr.Position = 0
Dim DecStr As New CryptoStream(mMemStr, _
DES.CreateDecryptor(Key, IV), CryptoStreamMode.Read)
Dim mStrRead As New StreamReader(DecStr)
Dim sRet as String
Try
sRet = mStrRead.ReadToEnd()
Catch ee As CryptographicException
MsgBox("Exception : " & ee.Message)
Exit Function
End Try
DecStr.Close()
mStrRead.Close()
mMemStr.Close()
Return sRet
End Function
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
1
by: Mark | last post by:
Hi - I have SSL, and need to collect credit card details for futher processing for a client. Although I have SSL, I still want to ensure the database holds encrypted data too - can anyone point...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
4
by: hohans | last post by:
Hi all, I have an encryption class that encrypts and decrypts password using TripleDESCryptoServiceProvider. It was written originally in framework 1.0 and been working fine. And those...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
1
by: SM | last post by:
Hi, I need your help, the following code is not working, ret = sr.ReadToEnd() throws the message error : Length of the data to decrypt is invalid. Any idea ? thank you public string...
0
by: FarooqRafiq | last post by:
Hi, My requirement is that a string is encrypted in VB.NET and sent to PHP, PHP decrypts the string (till here the logic is working) and then the PHP should encrtyp (where i am having problems)...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.