I hacked at the code a little to make it work. I don't know if this is
the way to go but it gets the string out in Unicode format. I don't
know off the top of my head how to display the text without spaces.
Actually It is a combination of some code I found the other day on
another list with some changes and your code pried into the middle of
it. Hope this helps.
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Module Module1
Sub Main()
Dim data As Byte() = ASCIIEncoding.ASCII.GetBytes("This is a
test data for Encrypt and Decrypt")
System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(data, 0, data.Length),
data.Length.ToString())
'Dim encrypted As Byte() = DesEncrypt(data)
'System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(encrypted, 0, encrypted.Length),
encrypted.Length.ToString())
' Dim decrypted As Byte() = DesDecrypt(encrypted)
'System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(decrypted, 0, decrypted.Length),
decrypted.Length.ToString())
Dim encrypted As String = DesEncrypt(data)
System.Console.WriteLine("{0} : length is {1}", encrypted,
encrypted.Length.ToString())
Dim decrypted As String = DesDecrypt(encrypted)
System.Console.WriteLine("{0} : length is {1}", decrypted,
decrypted.Length.ToString())
Dim message As String
message = "Hello"
message = Encrypt("Hello Joe")
System.Console.WriteLine(message)
System.Console.WriteLine(Decrypt(message))
Console.Read()
End Sub
Dim m_bDESKey As Byte() = ASCIIEncoding.ASCII.GetBytes("password")
Dim m_bDESIV As Byte() = ASCIIEncoding.ASCII.GetBytes("password")
Public Function DesEncrypt(ByVal data As Byte()) As String
Dim ms As MemoryStream = New MemoryStream(4096)
Dim des As DES = New DESCryptoServiceProvider()
Dim encStream As New CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV), CryptoStreamMode.Write)
encStream.Write(data, 0, data.Length)
encStream.FlushFinalBlock()
'calculate the length of the encrypted data
Dim bResult(ms.Position - 1) As Byte
ms.Position = 0
ms.Read(bResult, 0, bResult.Length)
encStream.Close()
Dim base64 As String
Return System.Convert.ToBase64String(bResult)
End Function
Public Function DesDecrypt(ByVal data As String) As String
Dim dataByte As Byte() = System.Convert.FromBase64String(data)
Dim ms As MemoryStream = New MemoryStream(dataByte.Length)
Dim des As DES = New DESCryptoServiceProvider()
Dim encStream As CryptoStream = New CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV), CryptoStreamMode.Read)
ms.Write(dataByte, 0, dataByte.Length)
ms.Position = 0
Dim strResult As String = New
StreamReader(encStream).ReadToEnd()
encStream.Close()
Return strResult
End Function
Dim Key As Byte() = ASCIIEncoding.ASCII.GetBytes("password")
Dim IV As Byte() = ASCIIEncoding.ASCII.GetBytes("password")
Private Function Encrypt(ByVal input As String) As String
Dim memStr As New System.IO.MemoryStream()
Dim bin As Byte() = Encoding.Unicode.GetBytes(input)
Dim des As New DESCryptoServiceProvider()
Dim encStream As New CryptoStream(memStr,
des.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
encStream.Write(bin, 0, bin.Length)
encStream.FlushFinalBlock()
'calculate the length of the encrypted data
Dim bResult(memStr.Position - 1) As Byte
memStr.Position = 0
memStr.Read(bResult, 0, bResult.Length)
encStream.Close()
Return Encoding.Unicode.GetString(bResult)
End Function
Private Function Decrypt(ByVal input As String) As String
'Make a new copy of the encrypted memory stream
Dim memStr2 As New
System.IO.MemoryStream(Encoding.Unicode.GetBytes(input))
Dim des2 As New DESCryptoServiceProvider()
Dim encStream2 As New CryptoStream(memStr2,
des2.CreateDecryptor(Key, IV), CryptoStreamMode.Read)
Dim strRdr As New StreamReader(encStream2, True)
Dim retStr As String = strRdr.ReadToEnd()
encStream2.Close()
Return retStr
End Function
End Module
-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED]] On Behalf Of Paul Ballard
Sent: Tuesday, June 11, 2002 10:23 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Encryption and Decryption using
MemoryStream
With FlushFinalBlock I still get garbage. I tried FlushFinalBlock and
Flush.
Any other ideas?
Paul Ballard
[EMAIL PROTECTED]
>From: Joseph E Shook <[EMAIL PROTECTED]>
>Reply-To: "Moderated discussion of advanced .NET topics."
><[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: [ADVANCED-DOTNET] Encryption and Decryption using
MemoryStream
>Date: Tue, 11 Jun 2002 09:09:43 -0700
>
>Try adding a encStream.FlushFinalBlock() just before the close()
method.
>
>-----Original Message-----
>From: Moderated discussion of advanced .NET topics.
>[mailto:[EMAIL PROTECTED]] On Behalf Of Paul Ballard
>Sent: Tuesday, June 11, 2002 7:48 AM
>To: [EMAIL PROTECTED]
>Subject: [ADVANCED-DOTNET] Encryption and Decryption using MemoryStream
>
>I am trying to do a quick encryption/decryption on strings. All of the
>examples I've seen involve encrypting whole files using FileStream. I
>have
>been attempting to simulate that functionality using MemoryStream.
>However,
>if I use an exact copy of the MemoryStream that I have encrypted as the
>source stream for decryption, the result is garbage.
>
>Is there a difference in the implementation of MemoryStream compared to
>FileStream that would cause this, or is there something that you can
see
>in
>the following code snippet that I am doing wrong? (Always a possibility
>:-))
>
> Dim memStr As New System.IO.MemoryStream()
> Private Function Encrypt(ByVal input As String) As String
> Dim bin As Byte() = New UnicodeEncoding().GetBytes(input)
> Dim des As New DESCryptoServiceProvider()
> Dim encStream As New CryptoStream(memStr,
>des.CreateEncryptor(Key,
>IV), CryptoStreamMode.Write)
> encStream.Write(bin, 0, bin.Length)
> encStream.Close()
> Return BitConverter.ToString(memStr.ToArray())
> End Function
>
> Private Function Decrypt(ByVal input As String)
> 'Make a new copy of the encrypted memory stream
> Dim memStr2 As New System.IO.MemoryStream(memStr.ToArray())
> Dim des2 As New DESCryptoServiceProvider()
> Dim encStream2 As New CryptoStream(memStr2,
>des2.CreateDecryptor(Key, IV), CryptoStreamMode.Read)
> Dim strRdr As New StreamReader(encStream2, True)
> Dim retStr As String = strRdr.ReadToEnd()
> encStream2.Close()
> Return retStr
> End Function
>
>Thanks for the help,
>Paul Ballard
>[EMAIL PROTECTED]
>
>
>_________________________________________________________________
>MSN Photos is the easiest way to share and print your photos:
>http://photos.msn.com/support/worldwide.aspx
>
>You can read messages from the Advanced DOTNET archive, unsubscribe
from
>Advanced DOTNET, or
>subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
>You can read messages from the Advanced DOTNET archive, unsubscribe
from
>Advanced DOTNET, or
>subscribe to other DevelopMentor lists at http://discuss.develop.com.
_________________________________________________________________
Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp.
You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.