Re: Encryption / decryption doesn't work

From: Ivan Medvedev [MS] (ivanmed@online.microsoft.com)
Date: 04/16/03


From: "Ivan Medvedev [MS]" <ivanmed@online.microsoft.com>
Date: Wed, 16 Apr 2003 09:39:18 -0700


Marc -
I have tried your code (with minor modifications) and it works fine for me.
What OS are you using? Perhaps the problem is that your OS does not support
strong encryption (if you open your Internet Explorer in Help/About you will
see the cipher strength, if it is < 128bit then that's the case). You may
want to try to download and install 128bit encryption pack from
www.microsoft.com.
Just in case below I am putting the vb code that compiles and runs fine for
me.
Hope this helps.
--Ivan

---------------------------------------------------
code ----------------------------------------------
Imports System.Security.Cryptography
Imports System
Imports System.IO
Imports System.Text

Module Encryption
    'Get the Host Name
    Private key() As Byte = {}
    Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
    Public Function Encrypt(ByVal stringToEncrypt As String, ByVal
SEncryptionKey As String) As String
        Try
            key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey)
            Dim des As New DESCryptoServiceProvider()
            Dim inputByteArray() As Byte =
System.Text.Encoding.UTF8.GetBytes(stringToEncrypt)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV),
CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

    Public Function Decrypt(ByVal stringToDecrypt As String, ByVal
sEncryptionKey As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        'Try
        key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey)
        Dim des As New DESCryptoServiceProvider()
        inputByteArray = Convert.FromBase64String(stringToDecrypt)
        Dim ms As New MemoryStream()
        Try
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV),
CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
        Catch CryptoError As Exception
            Dim c As String = CryptoError.Source & " " &
CryptoError.Message
        End Try
        Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
        Return encoding.GetString(ms.ToArray())
        'Catch e As Exception
        ' Return e.Message
        'End Try
    End Function

    Public Sub Main()

 Dim stringToEncrypt as String="washburn"
 Dim SEncryptionKey as String ="!#$a5MMA"

 Dim EncryptedString = Encrypt(stringToEncrypt, SEncryptionKey)
 System.Console.WriteLine(EncryptedString)

 Dim DecryptedString = Decrypt(EncryptedString,SEncryptionKey)
 System.Console.WriteLine(DecryptedString)

    End Sub

    End Module
--------------------------------------------------- end
code ----------------------------------------------

This posting is provided "AS IS" with no warranties, and confers no rights.

"Marc Massť" <mmasse@hartco.com> wrote in message
news:00a601c3037f$3ae82c50$a501280a@phx.gbl...
> I've seen Subject: Decryption does not work
> I got the same exact problem.
> And the Encryption doesn't work either...
>
> here is the code used
>
> stringToEncrypt="washburn"
> SEncryptionKey="!#$a5MMA"
>
> stringToDecrypt="67ZXCnbWgVN0tvc7wj9rAQ==" ?!! if good
> data ?
> SEncryptionKey="!#$a5MMA"
>
>
>
> Imports System.Security.Cryptography
> Imports System
> Imports System.IO
> Imports System.Xml
> Imports System.Text
>
> Module Encryption
> 'Get the Host Name
> Private key() As Byte = {}
> Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90,
> &HAB, &HCD, &HEF}
> Public Function Encrypt(ByVal stringToEncrypt As
> String, ByVal SEncryptionKey As String) As String
> Try
> key = System.Text.Encoding.UTF8.GetBytes(Mid
> (SEncryptionKey, 1, 8))
> Dim des As New DESCryptoServiceProvider()
> Dim inputByteArray() As Byte =
> System.Text.Encoding.UTF8.GetBytes(stringToEncrypt)
> Dim ms As New MemoryStream()
> Dim cs As New CryptoStream(ms,
> des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
> cs.Write(inputByteArray, 0,
> inputByteArray.Length - 1)
> cs.FlushFinalBlock()
> Return Convert.ToBase64String(ms.ToArray())
> Catch e As Exception
> Return e.Message
> End Try
> End Function
>
>
>
> Public Function Decrypt(ByVal stringToDecrypt As
> String, ByVal sEncryptionKey As String) As String
> Dim inputByteArray(stringToDecrypt.Length) As Byte
> 'Try
> key = System.Text.Encoding.UTF8.GetBytes(Mid
> (sEncryptionKey, 1, 8))
> Dim des As New DESCryptoServiceProvider()
> inputByteArray = Convert.FromBase64String
> (stringToDecrypt)
> Dim ms As New MemoryStream()
> Try
> Dim cs As New CryptoStream(ms,
> des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
> cs.Write(inputByteArray, 0,
> inputByteArray.Length)
> cs.FlushFinalBlock()
> Catch CryptoError As Exception
> Dim c As String = CryptoError.Source & " " &
> CryptoError.Message
> End Try
> Dim encoding As System.Text.Encoding =
> System.Text.Encoding.UTF8
> Return encoding.GetString(ms.ToArray())
> 'Catch e As Exception
> ' Return e.Message
> 'End Try
> End Function
>
>
> The error occurs to line
>
> Dim cs As New CryptoStream(ms, des.CreateDecryptor(key,
> IV), CryptoStreamMode.Write)
> cs.Write(inputByteArray, 0,
> inputByteArray.Length)
>
> if I look to cs , I got
>
> >? cs
> {System.Security.Cryptography.CryptoStream}
> System.IO.Stream:
> {System.Security.Cryptography.CryptoStream}
> _stream: {System.IO.MemoryStream}
> _Transform:
> {System.Security.Cryptography.CryptoAPITransform}
> _InputBuffer: {Length=8}
> _InputBufferIndex: 0
> _InputBlockSize: 8
> _NextInputBuffer: Nothing
> _NextInputBufferIndex: 0
> _TempInputBuffer: Nothing
> _OutputBuffer: {Length=8}
> _OutputBufferIndex: 0
> _OutputBlockSize: 8
> _transformMode: Write
> _canRead: False
> _canWrite: True
> _finalBlockTransformed: False
> CanRead: False
> CanSeek: False
> CanWrite: True
> Length: <error: an exception of type:
> {System.NotSupportedException} occurred>
> Position: <error: an exception of type:
> {System.NotSupportedException} occurred>
>
> It stopped working when I did install the WebService
> Service Pack and I got an application in production that
> stopped working...
>
> I would really appreciate Help
> Thanks in advance
>
>
>