473,386 Members | 1,705 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,386 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 2523
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.