Problem decrypting data

G

Gordon

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);
}
 
D

Duane Laflotte

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 address removed)
http://www.criticalsites.com/dlaflotte



Gordon said:
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);
}
 
G

Gordon

Duane,

That works! Thanks very much.

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

Gordon


Duane Laflotte said:
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 address removed)
http://www.criticalsites.com/dlaflotte



Gordon said:
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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top