Re: Encrypt code

From: Dan Fergus (dan@vbforest.com)
Date: 08/08/02


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
> > >
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: sound at wav-file
    ... Private Const SND_FILENAME = &H20000 ' name is a file name ... Public Sub PlaySoundFile(ByVal FileName As String, ... Thanks Karl. ... Good to hear, Harry. ...
    (microsoft.public.office.developer.vba)
  • Re: How to check double length?
    ... harry wrote: ... representation of that double). ... you know the class DecimalFormat. ... Are you interested the length of a String? ...
    (comp.lang.java.programmer)
  • Re: Remove Decimal Places
    ... string s = f.ToString.Replace; ... Kevin Spencer ... Microsoft MVP ... "Harry" wrote in message ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Is this a bug?
    ... "Harry" wrote in message ... Also using Decimal.TryParse with the above string returns false. ... In such a case, you would get zero on converting, and false for tryparse. ... Mike ...
    (microsoft.public.dotnet.languages.vb)