Re: Encrypt code
From: Dan Fergus (dan@vbforest.com)
Date: 08/08/02
- Next message: Ramiro Calderon: "Re: help with impersonation"
- Previous message: Sergius Adamtchouk: "help with impersonation"
- In reply to: Harry Simpson: "Re: Encrypt code"
- Next in thread: GreekToMe: "Re: Encrypt code"
- Reply: GreekToMe: "Re: Encrypt code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: "Dan Fergus" <dan@vbforest.com> Date: Thu, 8 Aug 2002 13:12:52 -0500
Harry,
You know I never noticed thbat error (I had it wrapped in a dummy try/catch)
until AFETR I had it published :-). I do not see why this is happening.
But you know, I removed the FlushFinalBlock call and it still works just
fine.
Dan
"Harry Simpson" <hssimpson@nospamphgt.net> wrote in message
news:uhyi#fYPCHA.1352@tkmsftngp11...
> 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
> > >
> > >
> > >
> >
> >
>
>
- Next message: Ramiro Calderon: "Re: help with impersonation"
- Previous message: Sergius Adamtchouk: "help with impersonation"
- In reply to: Harry Simpson: "Re: Encrypt code"
- Next in thread: GreekToMe: "Re: Encrypt code"
- Reply: GreekToMe: "Re: Encrypt code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|