|

Introduction
This article is an expansion on a few of the articles here on CodeProject. I noticed that there are a lot of articles and posts dealing with Cryptography in the .NET Framework. These were all well and good. They got me started. Then, as I was progressing and using the System.Security.Cryptography namespace, I noticed that if the file was the right size and padded correctly, even using a bad password would output a file. This was not acceptable to me. So, I set out to write a class that would allow me to encrypt and then decrypt/verify that the contents had been written correctly.
Background
These articles started me down the road of .NET Cryptography:
Since none of these verified the output, I wrote a class to fix this.
The Code
The EncryptFile method:
public static void EncryptFile(string inFile, string outFile,
string password, CryptoProgressCallBack callback)
{
using(FileStream fin = File.OpenRead(inFile),
fout = File.OpenWrite(outFile))
{
long lSize = fin.Length;
int size = (int)lSize;
byte[] bytes = new byte;
int read = -1;
int value = 0;
byte[] IV = GenerateRandomBytes(16);
byte[] salt = GenerateRandomBytes(16);
SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
sma.IV = IV;
fout.Write(IV,0,IV.Length);
fout.Write(salt,0,salt.Length);
HashAlgorithm hasher = SHA256.Create();
using(CryptoStream cout = new CryptoStream(fout,sma.CreateEncryptor(),
CryptoStreamMode.Write),
chash = new CryptoStream(Stream.Null,hasher,
CryptoStreamMode.Write))
{
BinaryWriter bw = new BinaryWriter(cout);
bw.Write(lSize);
bw.Write(FC_TAG);
while( (read = fin.Read(bytes,0,bytes.Length)) != 0 )
{
cout.Write(bytes,0,read);
chash.Write(bytes,0,read);
value += read;
callback(0,size,value);
}
chash.Flush();
chash.Close();
byte[] hash = hasher.Hash;
cout.Write(hash,0,hash.Length);
cout.Flush();
cout.Close();
}
}
}
What is interesting about this method and makes it different than the other articles' methods, is the fact that I write out the IV and Salt to the beginning of the output file. This adds a little more security to the file. For more information on these terms, check out Ritter's Crypto Glossary. Then after those two arrays are written, I encrypt and write the file size and a special tag (arbitrarily generated by me). These allow for some simple verifications of the file. After this, I do the encryption of the file, while hashing the data. Once the input file is completely encrypted, I encrypt the hash and write it out. By putting the hash at the end, I am able to verify the contents after decryption.
The DecryptFile method:
public static void DecryptFile(string inFile, string outFile,
string password, CryptoProgressCallBack callback)
{
using(FileStream fin = File.OpenRead(inFile),
fout = File.OpenWrite(outFile))
{
int size = (int)fin.Length;
byte[] bytes = new byte;
int read = -1;
int value = 0;
int outValue = 0;
byte[] IV = new byte[16];
fin.Read(IV,0,16);
byte[] salt = new byte[16];
fin.Read(salt,0,16);
SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password,salt);
sma.IV = IV;
value = 32;
long lSize = -1;
HashAlgorithm hasher = SHA256.Create();
using(CryptoStream cin = new CryptoStream(fin,sma.CreateDecryptor(),
CryptoStreamMode.Read),
chash = new CryptoStream(Stream.Null,hasher,
CryptoStreamMode.Write))
{
BinaryReader br = new BinaryReader(cin);
lSize = br.ReadInt64();
ulong tag = br.ReadUInt64();
if(FC_TAG != tag)
throw new CryptoHelpException("File Corrupted!");
long numReads = lSize / BUFFER_SIZE;
long slack = (long)lSize % BUFFER_SIZE;
for(int i = 0; i < numReads; ++i)
{
read = cin.Read(bytes,0,bytes.Length);
fout.Write(bytes,0,read);
chash.Write(bytes,0,read);
value += read;
outValue += read;
callback(0,size,value);
}
if(slack > 0)
{
read = cin.Read(bytes,0,(int)slack);
fout.Write(bytes,0,read);
chash.Write(bytes,0,read);
value += read;
outValue += read;
callback(0,size,value);
}
chash.Flush();
chash.Close();
fout.Flush();
fout.Close();
byte[] curHash = hasher.Hash;
byte[] oldHash = new byte[hasher.HashSize / 8];
read = cin.Read(oldHash,0,oldHash.Length);
if((oldHash.Length != read) || (!CheckByteArrays(oldHash,curHash)))
throw new CryptoHelpException("File Corrupted!");
}
if(outValue != lSize)
throw new CryptoHelpException("File Sizes don't match!");
}
}
During decryption, I reverse the actions of encryption. First, I read both the IV and Salt from the file. I use these to create the SymmetricAlgorithm. Second, I decrypt and read the file size and the tag. This is the first step in verification--if the tag is equal to the const tag in the class, I know the file is so far not corrupted. Now comes the decryption of the file data. This took a little work, because normally I would just keep reading from the file until I could not read anymore. But I put the hash at the end. So, I had to figure out how to read only the amount of data in the file size. I did this by using a little math: Number of Reads = The File Size / The Buffer Size
Left Over Bytes To Read = The File Size modulo The Buffer Size
NOTE: Both of these are integer math
Now, I use a for loop for reading most of the data, and then read the left over bytes.
During these reads, I hashed the decrypted data.
Then I read off the hash that was written last and compared it to the newly created hash. If they were equal, the file was not corrupted and the correct password was used to decrypt the file. If not, the algorithm has caught the error.
Using the code
This code is pretty easy to use: using nb;
public class TestClass
{
string myPassword = "TESTING!@#_123__";
string myPlainFile = "test.txt";
string myEncryptedFile = "test.encrypted";
string myDecryptedFile = "test.decrypted";
private static void Callback(int min, int max, int value)
{
}
[STAThread]
static void Main()
{
CryptoProgressCallBack cb = new CryptoProgressCallBack(Callback);
CryptoHelp.EncryptFile(myPlainFile, myEncryptedFile, myPassword, cb);
CryptoHelp.DecryptFile(myEncryptedFile,myDecryptedFile, myPassword, cb);
}
}
Points of Interest
The things I learned were how to use the .NET Framework's hashing/crypting algorithms. These classes when used in conjunction allow for some pretty powerful data security and data verification. I was able to verify the files data with a minimum overhead.
I hope that this is a help to others who need to add some security to their applications.
History
- 22 Oct 2004: 1.0 - Initial release.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh) | FirstPrevNext |
|
|
 |
|
|
I really liked this. It even works with binary files (ie: Word documents) they won't be corrupted afterwards.
Thanks Josef
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello,
I am searching for an php decryption function to decrypt the files that are encrypted with the program of this article ?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
You may also be interested in looking at the following, related Code Project articles:
Generic SymmetricAlgorithm Helper[^] This is a generic helper class that exposes simplified Encrypt and Decrypt functionality for strings, byte arrays and streams for any SymmetricAlgorithm derivative (DES, RC2, Rijndael, TripleDES, etc.).
Making TripleDES Simple in VB.NET and C#[^] This is a simple wrapper class that provides an easy interface for encrypting and decrypting byte arrays and strings using the 3DES algorithm.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi!
First of all Thank you for this great article!
I have a problem, and hope you can help! I dont know how to check, if the entered password for the decryption is right or wrong?
I realized if the password is wrong an exception is thrown: CryptoHelpException("File Corrupted!").
If im right how can i catch this exception from my code?? If im wrong what is the other way?
Thank you!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Wrap your code in a Try/Catch block:
try { // do something with the cryptohelp classes } catch(CryptoHelpException che) { // do something with the exception }
--------------------------- Hmmm... what's a signature?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Please help me on this padding problem. I m facing problem while decrypt of the file after the closing the curly barces of
"using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))"
It works fine when we add something in the encrypted file, but problem arises if i delete something from the encrypted file & tried to decrypt the file again.
Regards Pomey
public static void DecryptFileOnly(string filename, string filemask, string key, string fileExt) { try{ int index; string outFile; index = filename.LastIndexOf(fileExt); outFile = filename.Substring(0, index); //outFile = filename.Substring(0, filename.LastIndexOf(".")) + ".xml"; //************************************************************* // create and open the file streams using (FileStream fin = File.OpenRead(filename), fout = File.OpenWrite(outFile)) { int size = (int)fin.Length; // the size of the file for progress notification byte[] bytes = new byte[BUFFER_SIZE]; // byte buffer int read = -1; // the amount of bytes read from the stream
// read off the IV and Salt byte[] IV = new byte[16]; fin.Read(IV, 0, 16); byte[] salt = new byte[16]; fin.Read(salt, 0, 16);
// create the crypting stream SymmetricAlgorithm sma = CreateAlgo(key, salt); sma.IV = IV;
long lSize = -1; // the size stored in the input stream
// create the hashing object, so that we can verify the file HashAlgorithm hasher = SHA256.Create();
// create the cryptostreams that will process the file using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) { // read size from file BinaryReader br = new BinaryReader(cin); lSize = br.ReadInt64(); ulong tag = br.ReadUInt64();
if (FC_TAG != tag) throw new EncryptDecryptHelpException("File Corrupted!");
//determine number of reads to process on the file long numReads = lSize / BUFFER_SIZE;
// determine what is left of the file, after numReads long slack = (long)lSize % BUFFER_SIZE;
// read the buffer_sized chunks for (int i = 0; i < numReads; ++i) { read = cin.Read(bytes, 0, bytes.Length); fout.Write(bytes, 0, read); chash.Write(bytes, 0, read);
}
// now read the slack if (slack > 0) { read = cin.Read(bytes, 0, (int)slack); fout.Write(bytes, 0, read); chash.Write(bytes, 0, read);
}
// flush and close the hashing stream chash.Flush(); chash.Close();
// flush and close the output file fout.Flush(); fout.Close();
// read the current hash value byte[] curHash = hasher.Hash;
// get and compare the current and old hash values byte[] oldHash = new byte[hasher.HashSize / 8]; read = cin.Read(oldHash, 0, oldHash.Length); } }
//************************************** //File.Delete(filename); } catch(Exception ex) {
}
}
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
I get this error when i decrypt. I use this algorithm on xml files, this happens when i remove a node from the xml file. Any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I am trying to build a cryptography program using HMACSHA256 as the verification technique. I edited the code to use HMACSHA256 instead as well as C++. Upon decryption using the incorrect password, I will encounter this error.
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Additional information: Padding is invalid and cannot be removed.
Below is my code and the error occurs at this line while ( (bytesInCurrentBlock = cin->Read(buffer, 0, buffer->Length)) != 0 ) in the DecryptFile function. I would be grateful if someone could point out my mistake here.
PS: I am suppose to be expecting the hmacsha256 mismatch error to throw instead
void CryptoData::EncryptFile(String^ inFile, String^ outFile, String^ password, int keySize, CryptoProgressCallBack^ callback) { FileStream^ fin = gcnew FileStream(inFile, FileMode::Open, FileAccess::Read); FileStream^ fout = gcnew FileStream(outFile, FileMode::OpenOrCreate, FileAccess::Write);
fout->SetLength(0);
array<Byte>^ buffer = gcnew array<Byte>(BUFFER_SIZE); int bytesInCurrentBlock; long long bytesProcessed = 0; long long fileLength = fin->Length; long progress;
// generate IV and Salt array<Byte>^ IV = GenerateRandomBytes(16); array<Byte>^ salt = GenerateRandomBytes(16);
// create the crypting object SymmetricAlgorithm^ sma = CreateRijndaelManaged(password, keySize, IV, salt); // write the IV and salt to the beginning of the file fout->Write(IV, 0, IV->Length); fout->Write(salt, 0, salt->Length);
// Initialize the keyed hash object. HashAlgorithm^ hmacsha256 = gcnew HMACSHA256(sma->Key); // Compute the hash of the input file. array<Byte>^ hashValue = hmacsha256->ComputeHash(fin); // Reset fin to the beginning of the file. fin->Position = 0; // Write the computed hash value to the output file. fout->Write(hashValue, 0, hashValue->Length);
// create the cryptostream CryptoStream^ cout = gcnew CryptoStream(fout, sma->CreateEncryptor(), CryptoStreamMode::Write);
// read and write the bytes to the crypto stream in BUFFER_SIZE chunks while ( (bytesInCurrentBlock = fin->Read(buffer, 0, buffer->Length)) != 0 ) { cout->Write(buffer, 0, bytesInCurrentBlock); bytesProcessed += bytesInCurrentBlock; progress = (int)((bytesProcessed / fileLength) * 100); callback(0, 100, progress); }
// clear the hashing object hmacsha256->Clear();
// flush and close the cryptostream cout->Flush(); cout->Close();
// close the input file fin->Close(); }
void CryptoData::DecryptFile(String^ inFile, String^ outFile, String^ password, int keySize, CryptoProgressCallBack^ callback) { FileStream^ fin = gcnew FileStream(inFile, FileMode::Open, FileAccess::Read); FileStream^ fout = gcnew FileStream(outFile, FileMode::OpenOrCreate, FileAccess::Write);
fout->SetLength(0);
array<Byte>^ buffer = gcnew array<Byte>(BUFFER_SIZE); int bytesInCurrentBlock; long long bytesProcessed = 0; long long fileLength = fin->Length; int progress;
// read the IV and Salt array<Byte>^ IV = gcnew array<Byte>(16); fin->Read(IV, 0, 16); array<Byte>^ salt = gcnew array<Byte>(16); fin->Read(salt, 0, 16);
// create the crypting object SymmetricAlgorithm^ sma = CreateRijndaelManaged(password, keySize, IV, salt);
// Initialize the keyed hash object. HashAlgorithm^ hmacsha256 = gcnew HMACSHA256(sma->Key); // Create an array to hold the keyed hash value read from the file. array<Byte>^ storedHash = gcnew array<Byte>(hmacsha256->HashSize / 8); // Read in the storedHash. fin->Read(storedHash, 0, storedHash->Length); bytesProcessed = 64;
// create the cryptostream that will process the file CryptoStream^ cin = gcnew CryptoStream(fin, sma->CreateDecryptor(), CryptoStreamMode::Read);
// read the BUFFER_SIZE chunks while ( (bytesInCurrentBlock = cin->Read(buffer, 0, buffer->Length)) != 0 ) { fout->Write(buffer, 0, bytesInCurrentBlock); bytesProcessed += bytesInCurrentBlock; progress = (int)((bytesProcessed / fileLength) * 100); callback(0, 100, progress); }
// Reposition the file to compute the hash of the output file. fout->Position = 0; array<Byte>^ computedHash = hmacsha256->ComputeHash(fout);
if (!ByteArrayEqual(computedHash, storedHash)) { throw gcnew CryptoDataException("hmacsha256 mismatch"); } // flush and close the cryptostream cin->Flush(); cin->Close();
// close the output file fout->Close(); }
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi I am getting error while calling the decrypting function in a loop.The error is "Padding is invalid and cannot be removed".
I am getting this exception at this line
if (FC_TAG != tag) throw new CryptoHelpException("File Corrupted!");
Thanks in Advance
Regards Chandu.Sanka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello,
I think I need more information on what you are trying to do. The methods that I created do input file to output file all in one step. This for both variants--encrypt and decrypt.
Are you encrypting more than one file in a loop? Are you then trying to decrypt those newly encrypted files?
Can you post a very simple example code that shows your problem?
Thank you, Nathan
--------------------------- Hmmm... what's a signature?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Actually while I am using this code,in some cases only I am getting exception at the following code
if (FC_TAG != tag) throw new CryptoHelpException("File Corrupted!");
I mean the values of FC_TAG and tag are generating different values and this is resulting in throwing an exception.
Almost in all the cases I am getting the value of FC_TAG is 18158797384510146255.
Can you please tell me what might be the problem and how to resolve it.
Thanks in Advance Chandu.Sanka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
chandu.sanka wrote: Almost in all the cases I am getting the value of FC_TAG is 18158797384510146255.
Actually, I hope the FC_TAG == 18158797384510146255 ALL the time. If not your computer has more problems (like memory corruption or something). FC_TAG is defined in hexadecimal--not decimal. The number you stated is in decimal.
FC_TAG is a constant defined at the top of CryptoHelp class. This constant is used just as a verification that the file is being decrypted right and that this file was encrypted by the EncryptFile method.
If tag is coming back not equal to FC_TAG, then the file that you encrypted has become corrupt. I guess I would need an example file that encrypts and then fails to decrypt so that I could check my code for errors.
--------------------------- Hmmm... what's a signature?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
First to Nathan, MANY thanks for contributing your code to the .Net developer community. You've saved me hours of work.
I am currently working on a project that needs to work with encrypted xml files. For security reasons, I didn't want to write out to a temporary decrypted file, therefore I modified Nathan's Decrypt method to return a string. Here is the result:
public static string DecryptFile(string inFile, string password) { StringBuilder sb = new StringBuilder();
// create and open the file streams using(FileStream fin = File.OpenRead(inFile)) { long lSize = -1; int read = -1; int value = 32; int outValue = 0;
byte[] bytes = new byte[BUFFER_SIZE];
byte[] IV = new byte[16]; byte[] salt = new byte[16];
fin.Read(IV,0,16); fin.Read(salt,0,16);
SymmetricAlgorithm sma;
HashAlgorithm hasher = SHA256.Create();
sma = CryptoHelp.CreateRijndael(password,salt); sma.IV = IV;
using(CryptoStream cin = new CryptoStream(fin,sma.CreateDecryptor(),CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null,hasher,CryptoStreamMode.Write)) { BinaryReader br;
ulong tag;
br = new BinaryReader(cin); lSize = br.ReadInt64();
if((tag = br.ReadUInt64()) != FC_TAG) { throw new CryptoHelpException("File Corrupted!"); }
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
long numReads = lSize / BUFFER_SIZE; long slack = (long)lSize % BUFFER_SIZE;
for( int i = 0; i < numReads; ++i ) { string s;
read = cin.Read( bytes, 0, bytes.Length ); s = enc.GetString( bytes, 0, read );
sb.Append( s );
chash.Write( bytes, 0, read );
value += read; outValue += read; }
// now read the slack if( slack > 0 ) { string s;
read = cin.Read(bytes,0,(int)slack); s = enc.GetString( bytes, 0, read );
sb.Append( s );
chash.Write(bytes,0,read);
value += read; outValue += read; }
// flush and close the hashing stream chash.Flush(); chash.Close();
byte[] curHash = hasher.Hash; byte[] oldHash = new byte[hasher.HashSize / 8];
read = cin.Read(oldHash,0,oldHash.Length);
if((oldHash.Length != read) || (CheckByteArrays(oldHash, curHash)) == false ) { throw new CryptoHelpException("File Corrupted!"); } }
// make sure the written and stored size are equal if(outValue != lSize) { throw new CryptoHelpException("File Sizes don't match!"); }
return sb.ToString(); } }
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hello! I tried to path the encrypted file within Socket Connection. The Decryption proccess didn't show any problems, but after creating I couldn't open the decrypted file (that was originally in .jpg format). What could be a reson?
Eli Kremer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Eli,
I am not sure I understand what you mean. Did you encrypt the file into another file and then try to send that file across a socket connection? If your socket connection is working for every other type of file you are sending that would be weird. I would need to see code to help.
Later, Nathan
--------------------------- Hmmm... what's a signature?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Nathan,Thanks for your answer, Yes, I encrypted file into another, and sent it across the socket connection: Does the file corrupts when I open it for writing into networkstream on Server side and reading on client side? What is the simple way to avoid this ?
RECEIVING (Client Side): ////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
private void GetFile(string FileName, Socket sock ) { string FileEncryptedName; NetworkStream nfs = null; FileStream fout=null; receiveDone.WaitOne(5000,false); FileEncryptedName = FileName+".fcfe"; try { FileInfo fi = new FileInfo(PathToPlaceFiles+ "/" + FileEncryptedName); fout = new FileStream(fi.FullName , FileMode.Create, FileAccess.Write) ; nfs = new NetworkStream(sock) ; } catch (Exception ex) { this.clientLog.Text += " There may be problems with requested file" ; } int i=1; // Number of bytes to get from buffer in each cycle double duration=0; // transfer speed int rby=0 ; // Total number of bytes byte[] buffer; try { do { CurrentTime = DateTime.Now; TimeSpan timeLeft = (CurrentTime-StartTime ); labelDurationDetails.Text = timeLeft.TotalSeconds.ToString("F3") ; buffer = new byte[1024] ; //Read from the Network Stream if(nfs.CanRead) i = nfs.Read(buffer,0,buffer.Length) ; if(i>0) { fout.Write(buffer,0,i) ; rby=rby+i ; if(CurrentTime.Second > 0) duration = rby/CurrentTime.Second; duration/=1024; pBar.PerformStep(); labelRateDetails.Text = duration.ToString("F3"); this.clientLog.Text += ""; } }while( rby }catch(Exception ed) { MessageBox.Show("Problem in File Accept"+ed.Message); } try { FileName+=".fcfd"; FileInfo finput=new FileInfo(PathToPlaceFiles+ "/" + FileEncryptedName);
FileInfo foutput = new FileInfo(PathToPlaceFiles+ "/" + FileName); CryptoProgressCallBack cb = new CryptoProgressCallBack(this.ProgressCallBackDecrypt);
CryptoProgressCallBack(this.ProgressCallBackDecrypt);
CryptoHelp.DecryptFile(finput.FullName,foutput.FullName,"1",cb); } catch (Exception ed) { MessageBox.Show("Error in File Decryption"+ed.Message); }
////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // SENDING (Server Side) //////////////////////////////////////////////////////////////////////
public void ReadCallback(IAsyncResult ar) { // I perform some validation – xml “dialog” between client and server by “3-//way handshake” – that the needed file exists before sending file
try { StateObject state = (StateObject) ar.AsyncState; Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar); string content = Encoding.Unicode.GetString(state.buffer,0,bytesRead); //Getting the name of requested file if (content.IndexOf("") > -1) { //Getting the source file FileInfo file_info=LocateFile(content,1); CryptoProgressCallBack cb = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
CryptoHelp.EncryptFile(file_info.FullName,file_info.FullName+".fcfe","1",cb); //Getting the encrypted file file_info = LocateFile(content,2);
if( FileMessage(content) && file_info!=null ) { SendFile(file_info,handler); } } private FileInfo LocateFile(string content,int time) { //……………………… else if(time==2) { if(file.Name.Equals(doc.SelectSingleNode("//FileName").InnerText+".fcfe" )) return file; } //…………………………. } private void SendFile(FileInfo file_info, Socket handler) { // Writes file_info into Network Stream }
Eli
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
letoII wrote: private void GetFile(string FileName, Socket sock )
The do while loop seems to be miss formatted at the bottom. All it says is:
letoII wrote: }while( rby
It has no closing ')'. But from what I can determine, you don't know when to end the reading from the stream. Maybe if you send the number of bytes down the Socket in a long before actually sending the file, you could then read the long to tell you when you have read the required numbers of bytes.
-Nathan
--------------------------- Hmmm... what's a signature?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks!! It was written: }while(rbypBar.Maximum was set to cLength value, that was originally the length of the file,captured from XML, but I"ve forgotten, that now I"m sending the encrypted file, and cLength must be another value. I think now it must work perfect. The fact that I open the file to write in stream while sending and read from while getting, doesn't corrupts the file, isn't it? | | | | | |