Re: Encrypt code
From: Harry Simpson (hssimpson@nospamphgt.net)
Date: 08/06/02
- Next message: Harry Simpson: "AES flushfinalblock problem"
- Previous message: Kim Bach Petersen: "Re: FormsAuthentication and Redirects"
- In reply to: Dan Fergus: "Re: Encrypt code"
- Next in thread: Dan Fergus: "Re: Encrypt code"
- Reply: Dan Fergus: "Re: Encrypt code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
> >
> >
> >
>
>
- Next message: Harry Simpson: "AES flushfinalblock problem"
- Previous message: Kim Bach Petersen: "Re: FormsAuthentication and Redirects"
- In reply to: Dan Fergus: "Re: Encrypt code"
- Next in thread: Dan Fergus: "Re: Encrypt code"
- Reply: Dan Fergus: "Re: Encrypt code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|