Re: Simple Encryption/Decryption

From: Ivan Medvedev [MS] (ivanmed@online.microsoft.com)
Date: 01/04/03


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



Relevant Pages

  • Re: Simple Encryption/Decryption
    ... know is that when I pass the string to the encrypt method I get an encrypted ... cause .NET gives me a syntax error on the return line of the decrypt method. ... >>> rij.GenerateKey and rij.GenerateIV will make random keys and ivs. ...
    (microsoft.public.dotnet.security)
  • RE: 2007 User Level Security
    ... encrypted string. ... the way I use it is to encrypt user names and passwords and store the ... Dim prp As Property ... Dim dbs As Object, prp As Variant ...
    (microsoft.public.access.modulesdaovba)
  • Re: Workable encryption in Tcl??
    ... The longest string would be about the length ... How to encrypt a string ... nothing about binary formats. ... ># Perform ECB mode encryption on a plaintext block of BINARY data. ...
    (comp.lang.tcl)
  • RE: Using Win32 CryptDecrypt to Decrypt RijndaelManaged
    ... I figured out how to use RijndaelManaged with AES in the C++ app. ... C++ crypto WILL successfully decrypt the .NET generated ... I am trying to write a Win32 app that can decrypt that string using the ... I can get both to encrypt and decrypt successfully in their own projects, ...
    (microsoft.public.platformsdk.security)
  • In Search for the Proper Crypto System
    ... an asymetrical key cryptology. ... public/private key to encrypt only the symetric key used to encrypt the data ... the private key is eventually revealed. ... before A sends it to the first client, C1, and before any client sends it to ...
    (sci.crypt)

Quantcast