By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

CryptoStream?

P: n/a
Hi,

I want to use DES and CryptoStream to serialize a encrypted stream to a file
with a header "CRYPT". And I wrote these code:

To store:

FileStream fileStream = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] signture = AE.GetBytes("CRYPT");
fileStream.Write(signture, 0, 5);

ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Write);

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
formatter.Serialize(cryptoStream, MYDATA);

cryptoStream.Flush();
fileStream.Close();

To load:

FileStream fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
fileStream.Seek(5, SeekOrigin.Begin);
ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Read);
formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
MYDATA = (MYDATA)formatter.Deserialize(cryptoStream);
cryptoStream.Close();

And with that code I can not load the saved file. What's the problem?

And, what's the exact meaning of CryptoStream.Close()? If I replace the
CryptoStream.Flush() with Close(), it will crash. But the encrypted file
size is somewhat smaller than the original data size, is that mean a problem
in my code?

Thank you very much
weixiang
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Thank you. I'll test that ASAP.

What's the CryptoStream.Close() mean? It will throw a exception if I use
that to instead CryptoStream.Flush()

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
I haven't run the code, but I immediately noticed something that needs to
get fixed.
After the last write into the CrypoStream, you MUST call FlushFinalBlock
(not Flush).
The final block of cypher data must be properly padded according to the
cypher, otherwise, you can't decrypt.

-Rob [MVP]

"weixiang" <we******@yahoo.com> wrote in message
news:#C**************@tk2msftngp13.phx.gbl...
Hi,

I want to use DES and CryptoStream to serialize a encrypted stream to a

file
with a header "CRYPT". And I wrote these code:

To store:

FileStream fileStream = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] signture = AE.GetBytes("CRYPT");
fileStream.Write(signture, 0, 5);

ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Write);

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
formatter.Serialize(cryptoStream, MYDATA);

cryptoStream.Flush();
fileStream.Close();

To load:

FileStream fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
fileStream.Seek(5, SeekOrigin.Begin);
ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Read);
formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
MYDATA = (MYDATA)formatter.Deserialize(cryptoStream);
cryptoStream.Close();

And with that code I can not load the saved file. What's the problem?

And, what's the exact meaning of CryptoStream.Close()? If I replace the
CryptoStream.Flush() with Close(), it will crash. But the encrypted file size is somewhat smaller than the original data size, is that mean a

problem
in my code?

Thank you very much
weixiang


Nov 15 '05 #2

P: n/a
Close will close the CryptoStream and the underlying stream object the
CryptoStream is writing to.
In accordance to stream behavior, Close calls Flush, but in this case, you
need to call FlushFinalBlock.

-Rob [MVP]

"weixiang" <we******@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP12.phx.gbl...
Thank you. I'll test that ASAP.

What's the CryptoStream.Close() mean? It will throw a exception if I use
that to instead CryptoStream.Flush()

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
I haven't run the code, but I immediately noticed something that needs to
get fixed.
After the last write into the CrypoStream, you MUST call FlushFinalBlock
(not Flush).
The final block of cypher data must be properly padded according to the
cypher, otherwise, you can't decrypt.

-Rob [MVP]

"weixiang" <we******@yahoo.com> wrote in message
news:#C**************@tk2msftngp13.phx.gbl...
Hi,

I want to use DES and CryptoStream to serialize a encrypted stream to a
file
with a header "CRYPT". And I wrote these code:

To store:

FileStream fileStream = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] signture = AE.GetBytes("CRYPT");
fileStream.Write(signture, 0, 5);

ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Write);

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
formatter.Serialize(cryptoStream, MYDATA);

cryptoStream.Flush();
fileStream.Close();

To load:

FileStream fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
fileStream.Seek(5, SeekOrigin.Begin);
ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Read);
formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
MYDATA = (MYDATA)formatter.Deserialize(cryptoStream);
cryptoStream.Close();

And with that code I can not load the saved file. What's the problem?

And, what's the exact meaning of CryptoStream.Close()? If I replace

the CryptoStream.Flush() with Close(), it will crash. But the encrypted

file size is somewhat smaller than the original data size, is that mean a

problem
in my code?

Thank you very much
weixiang



Nov 15 '05 #3

P: n/a
So why doesn't Close call FlushFinalBlock? Why would you ever not want it
to?

S.

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
Close will close the CryptoStream and the underlying stream object the
CryptoStream is writing to.
In accordance to stream behavior, Close calls Flush, but in this case, you
need to call FlushFinalBlock.

-Rob [MVP]


Nov 15 '05 #4

P: n/a
I tried to call FlushFinalBlock to instead Flush, but it still throw a
exception that said "Length of the data to decrypt is invalid". Is there
other problem? And I believe I am going to encrypt my data, not to decrypt
it. Is that declared in the CryptoStream constructor with CSMode.Write?

Here is my current code:

FileStream fileStream = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] signture = AE.GetBytes("CRYPT");
fileStream.Write(signture, 0, 5);

ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Write);

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
formatter.Serialize(cryptoStream, MYDATA);

cryptoStream.FlushFinalBlock(); // throw exception here
fileStream.Close();
"Simon Trew" <ten.egnaro@werts> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
So why doesn't Close call FlushFinalBlock? Why would you ever not want it
to?

S.

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
Close will close the CryptoStream and the underlying stream object the
CryptoStream is writing to.
In accordance to stream behavior, Close calls Flush, but in this case, you need to call FlushFinalBlock.

-Rob [MVP]


Nov 15 '05 #5

P: n/a
OK, instead of trying to wade through the code, I wrote my own ;-)
This sample includes just about everything you need. I'm serializing a
string object, but that could easily be any object.

byte[] key = new byte[8];

byte[] iv = new byte[8];

FileStream fs = null;

CryptoStream cs = null;

BinaryFormatter bf = null;

string myData = "This is some test data.";

string myData2 = string.Empty;

DES desCypher = DES.Create();

// Encryption starts here

try

{

// create some key and IV data for the sample

RandomNumberGenerator.Create().GetBytes(key);

RandomNumberGenerator.Create().GetBytes(iv);

fs = new FileStream(@"C:\sample.dat", FileMode.OpenOrCreate);

fs.Write(Encoding.ASCII.GetBytes("CRYPT"), 0, 5);

cs = new CryptoStream(fs, desCypher.CreateEncryptor(key, iv),
CryptoStreamMode.Write);

bf = new BinaryFormatter();

bf.Serialize(cs, myData);

cs.FlushFinalBlock();

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

if (cs != null) cs.Close();

}

// decryption starts here

try

{

fs = new FileStream(@"C:\sample.dat", FileMode.Open);

fs.Position = 5;

cs = new CryptoStream(fs, desCypher.CreateDecryptor(key, iv),
CryptoStreamMode.Read);

myData2 = bf.Deserialize(cs) as string;

MessageBox.Show(myData2);

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

if (cs != null) cs.Close();

}

-Rob [MVP]
"weixiang" <we******@yahoo.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
I tried to call FlushFinalBlock to instead Flush, but it still throw a
exception that said "Length of the data to decrypt is invalid". Is there
other problem? And I believe I am going to encrypt my data, not to decrypt it. Is that declared in the CryptoStream constructor with CSMode.Write?

Here is my current code:

FileStream fileStream = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] signture = AE.GetBytes("CRYPT");
fileStream.Write(signture, 0, 5);

ICryptoTransform ct = des.CreateDecryptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fileStream, ct,
CryptoStreamMode.Write);

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DataSNBinder();
formatter.Serialize(cryptoStream, MYDATA);

cryptoStream.FlushFinalBlock(); // throw exception here
fileStream.Close();
"Simon Trew" <ten.egnaro@werts> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
So why doesn't Close call FlushFinalBlock? Why would you ever not want it
to?

S.

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
Close will close the CryptoStream and the underlying stream object the
CryptoStream is writing to.
In accordance to stream behavior, Close calls Flush, but in this case,

you need to call FlushFinalBlock.

-Rob [MVP]



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.