Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > Problem decrypting data

Reply
Thread Tools

Problem decrypting data

 
 
Gordon
Guest
Posts: n/a
 
      05-24-2005
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);
}

 
Reply With Quote
 
 
 
 
Duane Laflotte
Guest
Posts: n/a
 
      05-25-2005
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.velocityreviews.com/forums/(E-Mail Removed)
http://www.criticalsites.com/dlaflotte



"Gordon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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);
> }
>



 
Reply With Quote
 
 
 
 
Gordon
Guest
Posts: n/a
 
      06-02-2005
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
> (E-Mail Removed)
> http://www.criticalsites.com/dlaflotte
>
>
>
> "Gordon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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);
> > }
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encrypting/decrypting data laredotornado@zipmail.com Ruby 3 01-21-2008 10:09 PM
Decrypting DES encrypted text data from C/C++ using libcrypto (openssl) root@127.0.0.1 Java 1 10-14-2005 11:32 PM
Problem while decrypting the digitalsignature Dur ASP .Net Security 0 09-27-2005 01:30 PM
Encrypting/Decrypting Base64 encoded Aes Key with Dpapi Problem Phil C. ASP .Net Security 2 07-19-2005 08:57 PM
encrypting and decrypting with perl Marshall Dudley Perl 1 01-27-2005 06:14 PM



Advertisments