Problem decrypting data

Discussion in 'ASP .Net Security' started by Gordon, May 24, 2005.

  1. Gordon

    Gordon Guest

    I'm having a problem with the decryption part of the code below. An
    exception is generated when creating the CryptoStream for decryption. It's
    the error "Stream does not support seeking". I can't figure out what's
    causing the error...

    ASCIIEncoding textConverter = new ASCIIEncoding();
    byte[] bin = textConverter.GetBytes("some text to encrypt");

    RijndaelManaged tdes = new RijndaelManaged();

    tdes.GenerateKey();
    tdes.GenerateIV();
    byte[] bkey = tdes.Key;
    byte[] biv = tdes.IV;

    byte[] bout = new byte[100];
    byte[] bres = new byte[100];
    int count;

    try
    {
    using (MemoryStream msout = new MemoryStream(100))
    {
    CryptoStream encStream = new CryptoStream(msout,
    tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
    encStream.Write(bin, 0, bin.GetLength(0));
    encStream.FlushFinalBlock();
    msout.Seek(0, SeekOrigin.Begin);
    count = msout.Read(bout, 0, (int)msout.Length);
    }

    using (MemoryStream msin = new MemoryStream(bout))
    {
    CryptoStream encStream = new CryptoStream(msin,
    tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
    count = encStream.Read(bres, 0, (int)encStream.Length);
    }
    }
    catch (Exception e)
    {
    //HandleException(e);
    }
    Gordon, May 24, 2005
    #1
    1. Advertising

  2. Gordon,
    Looks like you may need to switch up a few lines here. Below I've played
    with your code and got this working. hope this helps.


    UTF8Encoding textConverter = new UTF8Encoding();
    byte[] bin = textConverter.GetBytes("some text to encrypt");

    RijndaelManaged tdes = new RijndaelManaged();

    tdes.GenerateKey();
    tdes.GenerateIV();
    byte[] bkey = tdes.Key;
    byte[] biv = tdes.IV;

    byte[] bout;
    byte[] bres;
    int count;

    try
    {
    using (MemoryStream msout = new MemoryStream())
    {
    CryptoStream encStream = new CryptoStream(msout,
    tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
    encStream.Write(bin, 0, bin.Length);
    encStream.Close();
    bout=msout.ToArray();
    //If you wanted the ciphered text inside bout you would do the
    following line
    string szCipheredText = Encoding.UTF8.GetString(bout);

    msout.Close();
    }

    using (MemoryStream msin = new MemoryStream(bout))
    {
    bres= new byte[bout.Length];
    CryptoStream encStream = new CryptoStream(msin,
    tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
    //To get the decrypted text use the next line
    encStream.Read(bres, 0, bout.Length);
    string szDecrypted = Encoding.UTF8.GetString(bres);
    System.Diagnostics.Debug.WriteLine(szDecrypted);
    }
    }
    catch (Exception ex)
    {
    //HandleException(e);
    }

    Hope this helps. If you still need to seek the streams let me know and I
    can try to modify the example I have here to do that instead of wholesale
    converting.

    P.S in next months .Net Developer Journal there is an article that explains
    a bit of the inner workings of Crypto in .NET :)
    ( http://dotnet.sys-con.com/general/currentcover.htm )
    --
    Duane Laflotte
    MCSE, MCSD, MCDBA, MCSA, MCT, MCP+I

    http://www.criticalsites.com/dlaflotte



    "Gordon" <> wrote in message
    news:...
    > I'm having a problem with the decryption part of the code below. An
    > exception is generated when creating the CryptoStream for decryption.

    It's
    > the error "Stream does not support seeking". I can't figure out what's
    > causing the error...
    >
    > ASCIIEncoding textConverter = new ASCIIEncoding();
    > byte[] bin = textConverter.GetBytes("some text to encrypt");
    >
    > RijndaelManaged tdes = new RijndaelManaged();
    >
    > tdes.GenerateKey();
    > tdes.GenerateIV();
    > byte[] bkey = tdes.Key;
    > byte[] biv = tdes.IV;
    >
    > byte[] bout = new byte[100];
    > byte[] bres = new byte[100];
    > int count;
    >
    > try
    > {
    > using (MemoryStream msout = new MemoryStream(100))
    > {
    > CryptoStream encStream = new CryptoStream(msout,
    > tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
    > encStream.Write(bin, 0, bin.GetLength(0));
    > encStream.FlushFinalBlock();
    > msout.Seek(0, SeekOrigin.Begin);
    > count = msout.Read(bout, 0, (int)msout.Length);
    > }
    >
    > using (MemoryStream msin = new MemoryStream(bout))
    > {
    > CryptoStream encStream = new CryptoStream(msin,
    > tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
    > count = encStream.Read(bres, 0, (int)encStream.Length);
    > }
    > }
    > catch (Exception e)
    > {
    > //HandleException(e);
    > }
    >
    Duane Laflotte, May 25, 2005
    #2
    1. Advertising

  3. Gordon

    Gordon Guest

    Duane,

    That works! Thanks very much.

    Sorry, it took a while for me to reply - Memorial Day vacation.... :)

    Gordon


    "Duane Laflotte" wrote:

    > Gordon,
    > Looks like you may need to switch up a few lines here. Below I've played
    > with your code and got this working. hope this helps.
    >
    >
    > UTF8Encoding textConverter = new UTF8Encoding();
    > byte[] bin = textConverter.GetBytes("some text to encrypt");
    >
    > RijndaelManaged tdes = new RijndaelManaged();
    >
    > tdes.GenerateKey();
    > tdes.GenerateIV();
    > byte[] bkey = tdes.Key;
    > byte[] biv = tdes.IV;
    >
    > byte[] bout;
    > byte[] bres;
    > int count;
    >
    > try
    > {
    > using (MemoryStream msout = new MemoryStream())
    > {
    > CryptoStream encStream = new CryptoStream(msout,
    > tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
    > encStream.Write(bin, 0, bin.Length);
    > encStream.Close();
    > bout=msout.ToArray();
    > //If you wanted the ciphered text inside bout you would do the
    > following line
    > string szCipheredText = Encoding.UTF8.GetString(bout);
    >
    > msout.Close();
    > }
    >
    > using (MemoryStream msin = new MemoryStream(bout))
    > {
    > bres= new byte[bout.Length];
    > CryptoStream encStream = new CryptoStream(msin,
    > tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
    > //To get the decrypted text use the next line
    > encStream.Read(bres, 0, bout.Length);
    > string szDecrypted = Encoding.UTF8.GetString(bres);
    > System.Diagnostics.Debug.WriteLine(szDecrypted);
    > }
    > }
    > catch (Exception ex)
    > {
    > //HandleException(e);
    > }
    >
    > Hope this helps. If you still need to seek the streams let me know and I
    > can try to modify the example I have here to do that instead of wholesale
    > converting.
    >
    > P.S in next months .Net Developer Journal there is an article that explains
    > a bit of the inner workings of Crypto in .NET :)
    > ( http://dotnet.sys-con.com/general/currentcover.htm )
    > --
    > Duane Laflotte
    > MCSE, MCSD, MCDBA, MCSA, MCT, MCP+I
    >
    > http://www.criticalsites.com/dlaflotte
    >
    >
    >
    > "Gordon" <> wrote in message
    > news:...
    > > I'm having a problem with the decryption part of the code below. An
    > > exception is generated when creating the CryptoStream for decryption.

    > It's
    > > the error "Stream does not support seeking". I can't figure out what's
    > > causing the error...
    > >
    > > ASCIIEncoding textConverter = new ASCIIEncoding();
    > > byte[] bin = textConverter.GetBytes("some text to encrypt");
    > >
    > > RijndaelManaged tdes = new RijndaelManaged();
    > >
    > > tdes.GenerateKey();
    > > tdes.GenerateIV();
    > > byte[] bkey = tdes.Key;
    > > byte[] biv = tdes.IV;
    > >
    > > byte[] bout = new byte[100];
    > > byte[] bres = new byte[100];
    > > int count;
    > >
    > > try
    > > {
    > > using (MemoryStream msout = new MemoryStream(100))
    > > {
    > > CryptoStream encStream = new CryptoStream(msout,
    > > tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
    > > encStream.Write(bin, 0, bin.GetLength(0));
    > > encStream.FlushFinalBlock();
    > > msout.Seek(0, SeekOrigin.Begin);
    > > count = msout.Read(bout, 0, (int)msout.Length);
    > > }
    > >
    > > using (MemoryStream msin = new MemoryStream(bout))
    > > {
    > > CryptoStream encStream = new CryptoStream(msin,
    > > tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
    > > count = encStream.Read(bres, 0, (int)encStream.Length);
    > > }
    > > }
    > > catch (Exception e)
    > > {
    > > //HandleException(e);
    > > }
    > >

    >
    >
    >
    Gordon, Jun 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marshall Dudley

    encrypting and decrypting with perl

    Marshall Dudley, Jan 27, 2005, in forum: Perl
    Replies:
    1
    Views:
    742
    Brian McCauley
    Jan 27, 2005
  2. root@127.0.0.1
    Replies:
    1
    Views:
    5,881
    Roedy Green
    Oct 15, 2005
  3. Phil C.
    Replies:
    2
    Views:
    438
    Phil C.
    Jul 19, 2005
  4. Dur
    Replies:
    0
    Views:
    169
  5. Replies:
    3
    Views:
    163
    laredotornado
    Jan 21, 2008
Loading...

Share This Page