Re: Encrypt code

From: Harry Simpson (hssimpson@nospamphgt.net)
Date: 08/06/02


From: "Harry Simpson" <hssimpson@nospamphgt.net>
Date: Tue, 6 Aug 2002 15:33:57 -0500


Dan,

Already found it and converted it to VB.NET. But getting errors on the
decrypt side:
"FlushFinalBlock() method was called twice on a CryptoStream. This method
can only be called once"
Even tried to rename object in Decrypt.
Here's my(your) converted code:

Public Shared Function EncryptString(ByVal src As String) As String
        Dim p As Byte() = Encoding.ASCII.GetBytes(src.ToCharArray())
        Dim encodedBytes() As Byte

        Dim ms As New MemoryStream()
        Dim rv As New RijndaelManaged()
        Dim cs As New CryptoStream(ms, rv.CreateEncryptor(keyb, ivb),
CryptoStreamMode.Write)

        Try
            cs.Write(p, 0, p.Length)
            cs.FlushFinalBlock()
            encodedBytes = ms.ToArray()
        Finally
            ms.Close()
            cs.Close()
        End Try

        Return Convert.ToBase64String(encodedBytes)
End Function 'EncryptString

Public Shared Function DecryptString(ByVal src As String) As String
        Dim p As Byte() = Convert.FromBase64String(src)
        Dim initialText() As Byte = New [Byte](p.Length) {}

        Dim rv As New RijndaelManaged()
        Dim ms2 As New MemoryStream(p)
        Dim cs2 As New CryptoStream(ms2, rv.CreateDecryptor(keyb, ivb),
CryptoStreamMode.Read)

        Try
            cs2.Read(initialText, 0, initialText.Length)
            cs2.FlushFinalBlock()
        Finally
            ms2.Close()
            cs2.Close()
        End Try

        Dim sb As New StringBuilder()

        Dim i As Integer

        While i < initialText.Length
            sb.Append(CChar(initialText(i).ToString))
        End While
        Return sb.ToString()
End Function 'DecryptString

Thanks
Harry

"Dan Fergus" <dan@vbforest.com> wrote in message
news:eoQTozXPCHA.2688@tkmsftngp11...
> HArry,
>
> This is in C# but should be easy to convert to VB.NET
>
> http://www.fawcette.com/vsm/2002_08/online/hottips/fergus/
>
> Dan Fergus
> www.forestsoftwaregroup.com
>
> "Harry Simpson" <hssimpson@nospamphgt.net> wrote in message
> news:#2VTSyLPCHA.1756@tkmsftngp08...
> > Is there any VB.NET code demonstrating a very easy to implement
> > encrypt/decrypt class.
> >
> > Such that i can basically pass in the string and the function spits out
> the
> > encrypted string and vice versa. I've searched the sites and found a
web
> > service and a few C# samples writing to file and decrypting..
> >
> > I just wanna translate in code behind or functions.
> >
> > TIA
> > Harry
> >
> >
> >
>
>



Relevant Pages

  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Encrypt and Decrypt a file using .NET 2.0?
    ... public static string GenerateKey() ... DES des = DES.Create; ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a problem with encryption
    ... >> That way I always get the original data I've encrypted. ... I really don't know a way to know how long the string I send to ... > encrypted data in a byte array trough network stream. ... to decrypt in one call, ...
    (microsoft.public.dotnet.general)
  • Help requested on FormsAuthentication.Decrypt causes System.Security.Cryptography.CryptographicExcep
    ... We need to be able to encrypt and decrypt large strings of up to maybe ... Public Shared Function Encrypt(ByVal UserData As String) As String ... the default section in machine.config, applications are ...
    (microsoft.public.vstudio.general)
  • Bad Data using TripleDES Encryption
    ... For example, when looping from 0 to 100, I encrypt and decrypt the values. ... public static string EncryptString(string toEncrypt) ... bytebytKey = GetLegalKey; ... CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); ...
    (microsoft.public.dotnet.framework.aspnet.security)

Loading