stavesacre Posted December 6, 2003 Posted December 6, 2003 I am having trouble sending (receiving?) a file over an encrypted networkstream. Here is my send/receive code. The program does not appear to be either sending or receiving the file, not sure which. Any thoughts!??! This is a major headache, 'cause it seems as if i'm just missing something simple. Let me know if you need any more information....or maybe if there's a better method. Thank-you in advance! public void sendFile(String fileName, Stream networkStream) { FileStream fin = new FileStream(fileName,FileMode.Open, FileAccess.Read); byte[] bin = new byte[100]; long rdlen = 0; long totlen = fin.Length; int len; CryptoStream encStream = new CryptoStream(networkStream, tripleDES.CreateEncryptor(), CryptoStreamMode.Write); Console.WriteLine("Encrypting..."); while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); } encStream.Close(); } --- public void receiveFile(FileStream fs, NetworkStream ns) { byte[] bin = new byte[100]; long rdlen = 0; int len = 100; CryptoStream decStream = new CryptoStream(fs, tripleDES.CreateDecryptor(), CryptoStreamMode.Write); Console.WriteLine("Decrypting..."); while(ns.DataAvailable) { len = ns.Read(bin, 0, 100); decStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); } decStream.Close(); } Quote
Administrators PlausiblyDamp Posted December 6, 2003 Administrators Posted December 6, 2003 In the receiveFile function shouldn't you be opening the decStream as CryptoStreamMode.Read ? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stavesacre Posted December 6, 2003 Author Posted December 6, 2003 Maybe i don't full understand CryptoStream? I am decrypting whatever I write to decStream. So I'm reading from the network stream into a byte array, then Writing that byte array to decStream, which writes it to the FileStream. Is that not the way to do it? Quote
Administrators PlausiblyDamp Posted December 6, 2003 Administrators Posted December 6, 2003 (edited) To be honest I've never tried writing to a decryption stream that way. I personally would have based the decStream on the network stream and decrypted the bytes as they arrive and just write out the raw bytes to the file stream. If you step through your code in the debugger does the client appear to send any data? If so is the receiving end getting it? edit: typo Edited May 4, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stavesacre Posted December 6, 2003 Author Posted December 6, 2003 actually. we have found that stepping through the code allows it to work. It has changed slighty, but still the same basic idea. Any thoughts now? Quote
Administrators PlausiblyDamp Posted December 6, 2003 Administrators Posted December 6, 2003 Could you post the changes? Also are you stepping through the code on the send or the receive (or both)? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stavesacre Posted December 6, 2003 Author Posted December 6, 2003 We've got it to the point where sometimes it works, and sometimes it doesn't. Which sounds like blocking/flushing problem? I'm not sure. Stepping through the receive code.... ---- public void sendFile(String fileName, Stream networkStream) { FileStream fin = new FileStream(fileName,FileMode.Open, FileAccess.Read); //Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. CryptoStream encStream = new CryptoStream(networkStream, tripleDES.CreateEncryptor(), CryptoStreamMode.Write); Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; //Console.WriteLine("{0} bytes processed", rdlen); } encStream.Close(); } --- public void receiveFile(FileStream fs, NetworkStream ns) { while(!ns.DataAvailable){} byte[] bin = new byte[100]; long rdlen = 0; int len = 100; //CryptoStream decStream = new CryptoStream(ns, tripleDES.CreateDecryptor(), CryptoStreamMode.Read); CryptoStream decStream = new CryptoStream(fs,tripleDES.CreateDecryptor(), CryptoStreamMode.Write); Console.WriteLine("Decrypting..."); while(len > 0) { //len = ns.Read(bin, 0, 100); //len = decStream.Read(bin, 0, len); len = ns.Read(bin, 0, len); rdlen = rdlen + len; decStream.Write(bin,0,len); //Console.WriteLine("{0} bytes processed", rdlen); } decStream.FlushFinalBlock(); decStream.Close(); //decStream.Close(); ns.Close(); fs.Close(); } Quote
Administrators PlausiblyDamp Posted December 6, 2003 Administrators Posted December 6, 2003 How are you declaring the variable tripleDES? Trying to do the same thing here and just want to make sure I'm getting things right. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stavesacre Posted December 6, 2003 Author Posted December 6, 2003 Class Var: TripleDESCryptoServiceProvider tripleDES; Constructor: tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = Encoding.Unicode.GetBytes("foobar12foob".ToCharArray()); tripleDES.IV = Encoding.Unicode.GetBytes("foob".ToCharArray()); Quote
Administrators PlausiblyDamp Posted December 6, 2003 Administrators Posted December 6, 2003 (edited) Just tried that here and it worked fine - the transferred file was identical to the original. However I was running both ends on the same PC. How large a file are you transferring? I tested it here with a couple of MP3s about 5M in size quite happily. When you sometimes it works and sometimes it doesn't how does it fail? Edited May 4, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stavesacre Posted December 31, 2003 Author Posted December 31, 2003 If it is a small file, it will not send the entire file. Quote
Administrators PlausiblyDamp Posted January 2, 2004 Administrators Posted January 2, 2004 Just tried it with my boot.ini file (322 bytes) - worked a treat sending code is class Class1 { /// /// The main entry point for the application. /// [sTAThread] static void Main(string[] args) { System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient("localhost",12345); Send s = new Send(); s.sendFile("c:\\boot.ini",tcp.GetStream()); } } public class Send { TripleDESCryptoServiceProvider tripleDES; public Send() { tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = Encoding.Unicode.GetBytes("foobar12foob".ToCharArray()); tripleDES.IV = Encoding.Unicode.GetBytes("foob".ToCharArray()); } public void sendFile(String fileName, Stream networkStream) { FileStream fin = new FileStream(fileName,FileMode.Open, FileAccess.Read); //Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. CryptoStream encStream = new CryptoStream(networkStream, tripleDES.CreateEncryptor(), CryptoStreamMode.Write); Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; //Console.WriteLine("{0} bytes processed", rdlen); } encStream.Close(); } } and receiving code is class Class1 { /// /// The main entry point for the application. /// [sTAThread] static void Main(string[] args) { // // TODO: Add code to start application here // Receive r = new Receive(); System.IO.FileStream fs = System.IO.File.OpenWrite("c:\\test.txt"); System.Net.Sockets.TcpListener tcp = new TcpListener(12345); tcp.Start(); r.receiveFile(fs,tcp.AcceptTcpClient().GetStream()); System.Console.ReadLine(); } } } public class Receive { TripleDESCryptoServiceProvider tripleDES; public Receive() { tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = Encoding.Unicode.GetBytes("foobar12foob".ToCharArray()); tripleDES.IV = Encoding.Unicode.GetBytes("foob".ToCharArray()); } public void receiveFile(FileStream fs, NetworkStream ns) { while(!ns.DataAvailable){} byte[] bin = new byte[100]; long rdlen = 0; int len = 100; CryptoStream decStream = new CryptoStream(fs,tripleDES.CreateDecryptor(), CryptoStreamMode.Write); Console.WriteLine("Decrypting..."); while(len > 0) { len = ns.Read(bin, 0, len); rdlen = rdlen + len; decStream.Write(bin,0,len); Console.WriteLine("{0} bytes read, {1} total bytes", len, rdlen); } decStream.FlushFinalBlock(); decStream.Close(); ns.Close(); fs.Close(); } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.