Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Encrypting and Decrypting a file

Encrypting and Decrypting a file

Scheduled Pinned Locked Moved C#
helpcomtutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Nirschl
    wrote on last edited by
    #1

    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!

    R 1 Reply Last reply
    0
    • P Peter Nirschl

      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!

      R Offline
      R Offline
      Rei Miyasaka
      wrote on last edited by
      #2

      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); }

      P 1 Reply Last reply
      0
      • R Rei Miyasaka

        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); }

        P Offline
        P Offline
        Peter Nirschl
        wrote on last edited by
        #3

        Thank you! You really helped me a lot! Kind regards, Peter Nirschl peter.nirschl@gmx.net

        R 1 Reply Last reply
        0
        • P Peter Nirschl

          Thank you! You really helped me a lot! Kind regards, Peter Nirschl peter.nirschl@gmx.net

          R Offline
          R Offline
          Rei Miyasaka
          wrote on last edited by
          #4

          :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups