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(file Name, FileMode.Create ,
FileAccess.Writ e, FileShare.None) ;
byte[] signture = AE.GetBytes("CR YPT");
fileStream.Writ e(signture, 0, 5);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fi leStream, ct,
CryptoStreamMod e.Write);
IFormatter formatter = new BinaryFormatter ();
formatter.Binde r = new DataSNBinder();
formatter.Seria lize(cryptoStre am, MYDATA);
cryptoStream.Fl ush();
fileStream.Clos e();
To load:
FileStream fileStream = new FileStream(file Name, FileMode.Open,
FileAccess.Read );
fileStream.Seek (5, SeekOrigin.Begi n);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fi leStream, ct,
CryptoStreamMod e.Read);
formatter = new BinaryFormatter ();
formatter.Binde r = new DataSNBinder();
MYDATA = (MYDATA)formatt er.Deserialize( cryptoStream);
cryptoStream.Cl ose();
And with that code I can not load the saved file. What's the problem?
And, what's the exact meaning of CryptoStream.Cl ose()? If I replace the
CryptoStream.Fl ush() 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 5 12927
Thank you. I'll test that ASAP.
What's the CryptoStream.Cl ose() mean? It will throw a exception if I use
that to instead CryptoStream.Fl ush()
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:uk******** ******@TK2MSFTN GP10.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******** ******@tk2msftn gp13.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(file Name, FileMode.Create , FileAccess.Writ e, FileShare.None) ; byte[] signture = AE.GetBytes("CR YPT"); fileStream.Writ e(signture, 0, 5);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv); CryptoStream cryptoStream = new CryptoStream(fi leStream, ct, CryptoStreamMod e.Write);
IFormatter formatter = new BinaryFormatter (); formatter.Binde r = new DataSNBinder(); formatter.Seria lize(cryptoStre am, MYDATA);
cryptoStream.Fl ush(); fileStream.Clos e();
To load:
FileStream fileStream = new FileStream(file Name, FileMode.Open, FileAccess.Read ); fileStream.Seek (5, SeekOrigin.Begi n); ICryptoTransfor m ct = des.CreateDecry ptor(key, iv); CryptoStream cryptoStream = new CryptoStream(fi leStream, ct, CryptoStreamMod e.Read); formatter = new BinaryFormatter (); formatter.Binde r = new DataSNBinder(); MYDATA = (MYDATA)formatt er.Deserialize( cryptoStream); cryptoStream.Cl ose();
And with that code I can not load the saved file. What's the problem?
And, what's the exact meaning of CryptoStream.Cl ose()? If I replace the CryptoStream.Fl ush() 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
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******** ******@TK2MSFTN GP12.phx.gbl... Thank you. I'll test that ASAP.
What's the CryptoStream.Cl ose() mean? It will throw a exception if I use that to instead CryptoStream.Fl ush()
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message news:uk******** ******@TK2MSFTN GP10.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******** ******@tk2msftn gp13.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(file Name, FileMode.Create , FileAccess.Writ e, FileShare.None) ; byte[] signture = AE.GetBytes("CR YPT"); fileStream.Writ e(signture, 0, 5);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv); CryptoStream cryptoStream = new CryptoStream(fi leStream, ct, CryptoStreamMod e.Write);
IFormatter formatter = new BinaryFormatter (); formatter.Binde r = new DataSNBinder(); formatter.Seria lize(cryptoStre am, MYDATA);
cryptoStream.Fl ush(); fileStream.Clos e();
To load:
FileStream fileStream = new FileStream(file Name, FileMode.Open, FileAccess.Read ); fileStream.Seek (5, SeekOrigin.Begi n); ICryptoTransfor m ct = des.CreateDecry ptor(key, iv); CryptoStream cryptoStream = new CryptoStream(fi leStream, ct, CryptoStreamMod e.Read); formatter = new BinaryFormatter (); formatter.Binde r = new DataSNBinder(); MYDATA = (MYDATA)formatt er.Deserialize( cryptoStream); cryptoStream.Cl ose();
And with that code I can not load the saved file. What's the problem?
And, what's the exact meaning of CryptoStream.Cl ose()? If I replace
the CryptoStream.Fl ush() 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
So why doesn't Close call FlushFinalBlock ? Why would you ever not want it
to?
S.
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:uU******** ******@TK2MSFTN GP11.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]
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(file Name, FileMode.Create ,
FileAccess.Writ e, FileShare.None) ;
byte[] signture = AE.GetBytes("CR YPT");
fileStream.Writ e(signture, 0, 5);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv);
CryptoStream cryptoStream = new CryptoStream(fi leStream, ct,
CryptoStreamMod e.Write);
IFormatter formatter = new BinaryFormatter ();
formatter.Binde r = new DataSNBinder();
formatter.Seria lize(cryptoStre am, MYDATA);
cryptoStream.Fl ushFinalBlock() ; // throw exception here
fileStream.Clos e();
"Simon Trew" <ten.egnaro@wer ts> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l... So why doesn't Close call FlushFinalBlock ? Why would you ever not want it to?
S.
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message news:uU******** ******@TK2MSFTN GP11.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]
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
RandomNumberGen erator.Create() .GetBytes(key);
RandomNumberGen erator.Create() .GetBytes(iv);
fs = new FileStream(@"C: \sample.dat", FileMode.OpenOr Create);
fs.Write(Encodi ng.ASCII.GetByt es("CRYPT"), 0, 5);
cs = new CryptoStream(fs , desCypher.Creat eEncryptor(key, iv),
CryptoStreamMod e.Write);
bf = new BinaryFormatter ();
bf.Serialize(cs , myData);
cs.FlushFinalBl ock();
}
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.Creat eDecryptor(key, iv),
CryptoStreamMod e.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******** ******@TK2MSFTN GP09.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(file Name, FileMode.Create , FileAccess.Writ e, FileShare.None) ; byte[] signture = AE.GetBytes("CR YPT"); fileStream.Writ e(signture, 0, 5);
ICryptoTransfor m ct = des.CreateDecry ptor(key, iv); CryptoStream cryptoStream = new CryptoStream(fi leStream, ct, CryptoStreamMod e.Write);
IFormatter formatter = new BinaryFormatter (); formatter.Binde r = new DataSNBinder(); formatter.Seria lize(cryptoStre am, MYDATA);
cryptoStream.Fl ushFinalBlock() ; // throw exception here fileStream.Clos e();
"Simon Trew" <ten.egnaro@wer ts> wrote in message news:%2******** **********@TK2M SFTNGP10.phx.gb l... So why doesn't Close call FlushFinalBlock ? Why would you ever not want
it to?
S.
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message news:uU******** ******@TK2MSFTN GP11.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]
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stingray |
last post by:
Are there any know problems with using a MemoryStream as a backing store for
a CryptoStream? I'm trying to simply encrypt and decrypt text in memory.
I'd like to create some simple methods to encrypt text before writing to a
database and decrypt it when reading it back. I figured I'd use a
MemoryStream that I can either store as a blob or convert to a string and
store as a varchar. Anyway, I can't get the CryptoStream to write to the...
|
by: Ayende Rahien |
last post by:
I've a problem in my code using NetworkStream.
I've a server which listen to a port, and then Read() from it, and a
client that send data.
My NetworkStream is wrapped in a CryptoStream, and my problem is that I
don't get all the data that I send.
When I use NetworkStream, all is working properly, but when I use
CryptoStream (initialize to use the NetworkStream), I only get partial data.
Anyone knows why this is happenning?
I would...
|
by: MattP |
last post by:
Ok, with the help of some examples found on the web and some minor
modifications on our own, we have a simple and working encrypt and
decrypt solution. It runs as a service, watches for files with a
specific extension in a specific directory. The files are uploaded by
FTP to this directory. The service then does the following steps:
1) Verify it can open the file (so we know it's fully uploaded).
2) Try to decrypt the file with known...
|
by: Daniel Weber |
last post by:
Hi!
In my code I create a CryptoStream around another stream of my own, with
CryptoStreamMode.Read. So I just can read from the CryptoStream. When I
close that CryptoStream, however, the underlying stream's Write() method is
called, with an empty buffer (offset and count-parameters are also 0). My
streams' CanWrite property returns false, so the CryptoStream should not
call Write(). It shouldn't call Write() at all, because I created it...
|
by: pesso |
last post by:
I have the following code that's taken and modified from a got_dot_net
example.
I'm trying to decrypt an Xml file that's been encrypted. I can dump the
decrypted stream to the console, but if I try to load that into
XmlTextReader, it throws XmlException. I'd appreciate your help.
//create file stream to read encrypted file back
FileStream fsread = new FileStream(args, FileMode.Open,
FileAccess.Read);
| |
by: semedao |
last post by:
Hi,
I am using cryptostream on both sides on tcp connection that pass data.
I am also use asyc socket , so , the data that recieved in the callback
method not always have the length of the buffer I sent.
for ex
I want to send 10000 bytes
I can get it in 10 times of 1000 bytes each.
so , I need to know when I complete the receiving , I want to write inside
cryptostream and check the position compare it to the length I already know
I...
|
by: TC |
last post by:
Hey All,
I posted this to the Crypto users group and forgot to add the VB.Net users
group. I apologize for any confusion.
I have been testing a try / catch / finally block and purposely raising
exceptions and I've noticed that if an exception of "Length of the data to
decrypt is invalid." is raised with the CryptoStream object, later this
exception will get raised a second time and thrown to the caller when trying
to close the stream...
|
by: =?Utf-8?B?TWFydGluIE1hZHJlemE=?= |
last post by:
Hi,
i've got problems with CryptoStream and GZipStream. Someone tried that? Or
got any idea why this wont work?
i tried every combination, first i used CryptoStream, after that GZipStream.
Or first i create a file with GZipStream and crypt that file. But become back
the orginal is impossible...
I hope anyone got an idea how this could work.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |