Home All Groups Group Topic Archive Search About

DESCryptoServiceProvider

Author
2 Mar 2005 2:24 PM
Ondrej Sevecek
Hello,

would you please provide me with some simple sample of how to use the
DESCryptoServiceProvider to encrypt a buffer

byte[] buffer;

with key

byte[] key;

I saw some sample using Streams, but is there a simpler method working for
buffers?

O.

Author
2 Mar 2005 5:09 PM
Dominick Baier [DevelopMentor]
Hello Ondrej Sevecek" ondra_at_sevecek_dt_com,

you have to use CryptoStream. You could couple it with a MemoryStream to
do it all in-Memory buffer based.

if have an example here
http://www.leastprivilege.com/PermaLink.aspx?guid=f73ca1e0-bcfa-4563-862f-eb06ab317075

which uses RijndaelManaged and a FileStream. Should be easy to modify

Dominick Baier - DevelopMentor
www.leastprivilege.com

Show quoteHide quote
> Hello,
>
> would you please provide me with some simple sample of how to use the
> DESCryptoServiceProvider to encrypt a buffer
>
> byte[] buffer;
>
> with key
>
> byte[] key;
>
> I saw some sample using Streams, but is there a simpler method working
> for buffers?
>
> O.
>
Author
5 Mar 2005 12:31 AM
Valery Pryamikov
Working with buffers... something like following shall do it (not sure if it
simplier :-):

    SymmetricAlgorithm algorithm = DES.Create();
    ICryptoTransform encryptor = algorithm.CreateEncryptor();
    int blockByteSize = algorithm.BlockSize / 8;
    int nBlocks = ((data.Length+blockByteSize-1)/blockByteSize);
    Byte[] encryptedData = new Byte[nBlocks*blockByteSize];
    for (int i = 0; i < nBlocks; i++)
    {
     if (i == nBlocks - 1)
     {
      Byte[] lastBlock = encryptor.TransformFinalBlock(data, i *
blockByteSize, nBlocks * blockByteSize - data.Length);
      Array.Copy(lastBlock, 0, encryptedData, i * blockByteSize,
lastBlock.Length);
     }
     else
     {
      encryptor.TransformBlock(
       data, i * blockByteSize, blockByteSize,
       encryptedData, i * blockByteSize);
     }
    }

-Valery
http://www.harper.no/valery

Show quoteHide quote
"Ondrej Sevecek" <ondra_at_sevecek_dt_com> wrote in message
news:%23V6zZLzHFHA.896@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> would you please provide me with some simple sample of how to use the
> DESCryptoServiceProvider to encrypt a buffer
>
> byte[] buffer;
>
> with key
>
> byte[] key;
>
> I saw some sample using Streams, but is there a simpler method working for
> buffers?
>
> O.
>
>