Encrypting and Decrypting a file
-
Hello everyone, I have a problem with Encrypting and Decrypting files. I read an article about it at the Microsoft-Homepage but this example works only with text files and I want to encrypt and decrypt every file type. http://support.microsoft.com/default.aspx?scid=kb;EN-US;307010[^]. Maybe the problem is Encoding. Please help me!
-
Hello everyone, I have a problem with Encrypting and Decrypting files. I read an article about it at the Microsoft-Homepage but this example works only with text files and I want to encrypt and decrypt every file type. http://support.microsoft.com/default.aspx?scid=kb;EN-US;307010[^]. Maybe the problem is Encoding. Please help me!
Actually, the encrypt function will work with any file type, but the decrypt function might not. StreamReader and StreamWriter are to help with reading/writing text from/to streams, so you don't want to use them in this case. The example code there isn't very efficient for large files either, since it reads in the whole file at once, and your memory will be stuffed. Here's a more practical version of the last section of the EncryptFile function:
byte[] buffer = new byte[1024]; int len; //Read in a maximum of 1 kilobyte from the file at a time, while there are more than 0 bytes left. while((len = fsInput.Read(buffer, 0, buffer.Length)) != 0) { //Write the contents of the current buffer to the crypt stream cryptostream.Write(buffer, 0, len); } cryptostream.Close(); fsInput.Close(); fsEncrypted.Close();
And here's the last 5 lines of the DecryptFile function, works for any file type and file size://Print the contents of the decrypted file. byte[] buffer = new byte[1024]; int len; FileStream fsDecrypted = new FileStream(sOutputFilename, FileMode.Create); //Read in a maximum of 1 kilobyte at a time, while there are more than 0 bytes left while((len = cryptostreamDecr.Read(buffer, 0, buffer.Length)) != 0) { //Write the contents of the decrypted buffer to the output file stream fsDecrypted.Write(buffer, 0, len); }
-
Actually, the encrypt function will work with any file type, but the decrypt function might not. StreamReader and StreamWriter are to help with reading/writing text from/to streams, so you don't want to use them in this case. The example code there isn't very efficient for large files either, since it reads in the whole file at once, and your memory will be stuffed. Here's a more practical version of the last section of the EncryptFile function:
byte[] buffer = new byte[1024]; int len; //Read in a maximum of 1 kilobyte from the file at a time, while there are more than 0 bytes left. while((len = fsInput.Read(buffer, 0, buffer.Length)) != 0) { //Write the contents of the current buffer to the crypt stream cryptostream.Write(buffer, 0, len); } cryptostream.Close(); fsInput.Close(); fsEncrypted.Close();
And here's the last 5 lines of the DecryptFile function, works for any file type and file size://Print the contents of the decrypted file. byte[] buffer = new byte[1024]; int len; FileStream fsDecrypted = new FileStream(sOutputFilename, FileMode.Create); //Read in a maximum of 1 kilobyte at a time, while there are more than 0 bytes left while((len = cryptostreamDecr.Read(buffer, 0, buffer.Length)) != 0) { //Write the contents of the decrypted buffer to the output file stream fsDecrypted.Write(buffer, 0, len); }
Thank you! You really helped me a lot! Kind regards, Peter Nirschl peter.nirschl@gmx.net
-
Thank you! You really helped me a lot! Kind regards, Peter Nirschl peter.nirschl@gmx.net
:)