C#: encrypting a compressed file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    C#: encrypting a compressed file

    i have a xml file ..around 200kb (max) .. i compressed it .. using GZipStream class. Now i wanna encrypt it.... Is it right? or the other way round... First encrypt and then compress.... Also i wanna know some good encrytion class in system.security .cryprto... i zeroed on
    Rijndael,

    http://msdn2.microsoft .com/en-us/library/system.security .cryptography.r ijndael.aspx

    the data is impt... though not THAT impt.. still i wanna go with wat can be VERY secure..
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Originally posted by dirtbag
    i have a xml file ..around 200kb (max) .. i compressed it .. using GZipStream class. Now i wanna encrypt it.... Is it right? or the other way round... First encrypt and then compress.... Also i wanna know some good encrytion class in system.security .cryprto... i zeroed on
    Rijndael,

    http://msdn2.microsoft .com/en-us/library/system.security .cryptography.r ijndael.aspx

    the data is impt... though not THAT impt.. still i wanna go with wat can be VERY secure..
    I believe that you are able to compress encrypted data, but I would be surprized if you could do the reverse. Personally, I would not compress encrypted data and would treat compressed and encrypted data separately. I don't believe that compression will add to your data security but it could affect your data integrity.

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      The original file is XML file ..(serialized .. let say like person class containing List<string> as fields)
      as of now i have compressed it
      Code:
      using (GZipStream mygz = new GZipStream(fs, CompressionMode.Decompress))
                  {
      
                      const int buf_size = 4096;
                      byte[] buffer = new byte[buf_size];
                      int bytes_read = 0;
      
                      do
                      {
                          bytes_read = mygz.Read(buffer, 0, buf_size);
                          fsd.Write(buffer, 0, bytes_read);
                      } while (bytes_read != 0);
                  }
      sample code....
      the grt thing abt this is that the size is reduced a lot... Which is quite IMPORTANT to me as of now...
      After compressing i encrypted it...
      using RijndaelManaged class...
      I have run my program...no probs as of now....
      If i do the other way round... Encrypting it first... the file size increases... Actually i tried to encrypt it with
      System.Security .Cryptography.X ml;
      The file got lot larger.... maybe i need to try reverse to see with RijndaelManaged class

      wow... hmm wat u say it right.... i need to encrypt it first... it makes no sense to
      encrypt a compressed file... its the REAL info needs to be encrypted... then maybe i can compress it...
      Last edited by Frinavale; Dec 11 '13, 06:48 PM. Reason: Added code tags

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by kenobewan
        I believe that you are able to compress encrypted data, but I would be surprized if you could do the reverse. Personally, I would not compress encrypted data and would treat compressed and encrypted data separately. I don't believe that compression will add to your data security but it could affect your data integrity.
        You can compress encrypted data, you can also encrypt compressed data. However compressing encrypted data can have adverse effects on your data integrity as kenobewan said. If you are confident that your compression algorithm is good and has a low rate of corrupting data then you might feel more comfortable doing so.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by dirtbag
          wow... hmm wat u say it right.... i need to encrypt it first... it makes no sense to
          encrypt a compressed file... its the REAL info needs to be encrypted... then maybe i can compress it...
          With some encryption algorithms the source information needs to be padded thereby increasing your file size. When you encrypt a compressed file the REAL info is still encrypted, its not like I could take your encrypted zip file and decompress it and suddenly your files would be clear text again.

          Comment

          Working...