Re: Simple Encryption/Decryption

From: Michael Giagnocavo (mggUNSPAM@Atrevido.net)
Date: 01/03/03


From: "Michael Giagnocavo" <mggUNSPAM@Atrevido.net>
Date: Fri, 3 Jan 2003 11:00:49 -0600


What syntax error does it give you? Are you sure the code is typed in
correctly?

Also, it will help if you read up on the docs for the
system.security.cryptograpgy namespace, that will help you a lot.
-mike

"Ken" <admin@kabwebs.com> wrote in message
news:OyNG4UzsCHA.2516@TK2MSFTNGP09...
> Thanks for all your help so far, but I am clueless how this works, all I
do
> know is that when I pass the string to the encrypt method I get an
encrypted
> string back. I am new to this cryptology stuff and I don't really
understand
> any of it. When I try the Decrypt method I can't even run the application
> cause .NET gives me a syntax error on the return line of the decrypt
method.
>
> Ken
>
> "Michael Giagnocavo" <mggUNSPAM@Atrevido.net> wrote in message
> news:eEFm2MrsCHA.456@TK2MSFTNGP09...
> > Where are you assigning the keys and ivs? if they are different, it
will
> > come out incorrectly.
> > -mike
> >
> > "Ken" <admin@kabwebs.com> wrote in message
> > news:#1xHjOpsCHA.1644@TK2MSFTNGP12...
> > > This is the code I have. Encrypt seems to work fine, but decrypt is a
> > mess.
> > > I think I may not have understood what you told me to switch?
> > >
> > > public string encrypt(string s)
> > >
> > > {
> > >
> > > byte[] inbuf = System.Text.Encoding.UTF8.GetBytes(s); // utf8 allows
you
> > to
> > > encode unicode.
> > >
> > > // maybe using ascii would provide better performance if you don't use
> any
> > > extended characters...
> > >
> > > byte[] outbuf;
> > >
> > > RijndaelManaged rij = new RijndaelManaged();
> > >
> > > //rij.Key = // note that you will need the same key and iv to
> enc/de-enc.
> > >
> > > //rij.IV = // see docs for more info on this
> > >
> > >
> > > outbuf = rij.CreateEncryptor().TransformFinalBlock(inbuf, 0,
> > inbuf.Length);
> > >
> > > // outbuf now contains the encrypted bytes. CreateEncryptor creates an
> > > ICryptoTransform
> > >
> > > return System.Convert.ToBase64String(outbuf);
> > >
> > > // base64 is a nice way to move around your encypted data in places
> where
> > > using byte arrays is more difficult (databases, text files, etc.).
> > >
> > > }
> > >
> > > public string decrypt(string s)
> > >
> > > {
> > >
> > > byte[] inbuf = System.Convert.FromBase64String(s); // utf8 allows you
to
> > > encode unicode.
> > >
> > > // maybe using ascii would provide better performance if you don't use
> any
> > > extended characters...
> > >
> > > byte[] outbuf;
> > >
> > > RijndaelManaged rij = new RijndaelManaged();
> > >
> > > //rij.Key = // note that you will need the same key and iv to
> enc/de-enc.
> > >
> > > //rij.IV = // see docs for more info on this
> > >
> > >
> > > outbuf = rij.CreateDecryptor().TransformFinalBlock(inbuf, 0,
> > inbuf.Length);
> > >
> > > // outbuf now contains the encrypted bytes. CreateEncryptor creates an
> > > ICryptoTransform
> > >
> > > return System.Text.Encoding.UTF8.GetBytes(outbuf);
> > >
> > > // base64 is a nice way to move around your encypted data in places
> where
> > > using byte arrays is more difficult (databases, text files, etc.).
> > >
> > > }
> > >
> > > "Michael Giagnocavo" <mggUNSPAM@Atrevido.net> wrote in message
> > > news:eB1hGHosCHA.868@TK2MSFTNGP12...
> > > > rij.GenerateKey and rij.GenerateIV will make random keys and ivs.
> they
> > > will
> > > > be in the key and iv properties of that instance.
> > > > -mike
> > > >
> > > > "Ken" <admin@kabwebs.com> wrote in message
> > > > news:uLF9UnmsCHA.2036@TK2MSFTNGP12...
> > > > > Thanks Mike!
> > > > >
> > > > > What is a key and where do I get one or how do I make one?
> > > > >
> > > > > Ken
> > > > > "Michael Giagnocavo" <mggUNSPAM@Atrevido.net> wrote in message
> > > > > news:OnhR#disCHA.2592@TK2MSFTNGP10...
> > > > > > here is some sample code as far as my memory goes. my shift key
> is
> > > bad,
> > > > > so
> > > > > > make sure you case it correctly.
> > > > > >
> > > > > > using system;
> > > > > > using system.security.cryptography;
> > > > > > public string encrypt(string s) {
> > > > > > byte[] inbuf = system.text.encoding.utf8.getbytes(s); //
utf8
> > > > allows
> > > > > > you to encode unicode.
> > > > > > // maybe using ascii would provide better
> > performance
> > > if
> > > > > you
> > > > > > don't use any extended characters...
> > > > > > byte[] outbuf;
> > > > > > rijndaelmanaged rij = new rijndaelmanaged();
> > > > > > rij.key = somebytearray[]; // note that you will
need
> > the
> > > > same
> > > > > > key and iv to enc/de-enc.
> > > > > > rij.iv = someotherbytearray[]; // see docs for more info
on
> > > this
> > > > > > outbuf = rij.CreateEncryptor().TransformFinalBlock(inbuf, 0,
> > > > > > inbuf.length);
> > > > > > // outbuf now contains the encrypted bytes. CreateEncryptor
> > > creates
> > > > > an
> > > > > > ICryptoTransform
> > > > > > return system.convert.ToBase64String(outbuf);
> > > > > > // base64 is a nice way to move around your encypted data in
> > > places
> > > > > > where using byte arrays is more difficult (databases, text
files,
> > > etc.).
> > > > > > }
> > > > > >
> > > > > > its the same for decrypt, just change CreateEncryptor to
> > > > createdecryptor,
> > > > > > and switch the positions of the base64 and utf8 conversions.
> > > > > >
> > > > > > good luck,
> > > > > > -mike
> > > > > >
> > > > > > "Ken" <admin@kabwebs.com> wrote in message
> > > > > > news:u43upORsCHA.1676@TK2MSFTNGP10...
> > > > > > > Hi all,
> > > > > > >
> > > > > > > I am new to cryptology and I am looking for a simple way to
take
> a
> > > > > string
> > > > > > of
> > > > > > > say 15 characters pass it to a class that will encrypt or
> decrypt
> > > the
> > > > > > string
> > > > > > > and return the value in a string. Someone mentioned a Byte
Array
> > for
> > > > > small
> > > > > > > amounts of data. Can some explain how to use this? Are there
any
> > > good
> > > > > > > samples anywhere?
> > > > > > >
> > > > > > > Thanks in advance!
> > > > > > >
> > > > > > > Ken
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>