Re: Simple Encryption/Decryption
From: Ivan Medvedev [MS] (ivanmed@online.microsoft.com)
Date: 01/04/03
- Next message: Chris Morrison: "Digital signatures in ZIP files"
- Previous message: Ivan Medvedev [MS]: "Re: RSA Decryption Error- HELP!"
- In reply to: Ken: "Re: Simple Encryption/Decryption"
- Next in thread: Ken: "Re: Simple Encryption/Decryption"
- Reply: Ken: "Re: Simple Encryption/Decryption"
- Reply: Ken: "Re: Simple Encryption/Decryption"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: "Ivan Medvedev [MS]" <ivanmed@online.microsoft.com> Date: Fri, 3 Jan 2003 15:54:09 -0800
Ken -
the error is valid because you are trying to get bytes out of bytes. You
probably ment to use GetString there instead of GetBytes.
Also, you might want to take a look at the sample code I have just posted to
this group.
Thanks,
--Ivan
"Ken" <admin@kabwebs.com> wrote in message
news:uP1$Bm2sCHA.2592@TK2MSFTNGP10...
> This is the error.
> C:\Cryptology1\clsEncrpt.cs(55): The best overloaded method match for
> 'System.Text.Encoding.GetBytes(char[])' has some invalid arguments
>
> I did try to read the documentation before I posted the first message
here.
> I just don't understand what is happeneing. Cryptology is all new to me. I
> have never written an application in my last 12 years of vb development
> where I had to encrypt anything. To make matters worse, I am very new to
C#
> also! Thanks!
>
> Ken
>
> "Michael Giagnocavo" <mggUNSPAM@Atrevido.net> wrote in message
> news:eNUDfs0sCHA.2648@TK2MSFTNGP09...
> > 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
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
- Next message: Chris Morrison: "Digital signatures in ZIP files"
- Previous message: Ivan Medvedev [MS]: "Re: RSA Decryption Error- HELP!"
- In reply to: Ken: "Re: Simple Encryption/Decryption"
- Next in thread: Ken: "Re: Simple Encryption/Decryption"
- Reply: Ken: "Re: Simple Encryption/Decryption"
- Reply: Ken: "Re: Simple Encryption/Decryption"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|