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\Decrypting

Encrypting\Decrypting

Scheduled Pinned Locked Moved C#
security
3 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.
  • D Offline
    D Offline
    draco_iii
    wrote on last edited by
    #1

    I am using the following to open and write and encryped datafile. FileStream fout = new FileStream(OptionsDataFile, FileMode.Create, FileAccess.Write); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Write); ASCIIEncoding AE = new ASCIIEncoding(); and the following to decrypt. FileStream fin = new FileStream(OptionsDataFile, FileMode.Open, FileAccess.Read); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fin, tdes.CreateDecryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Read); ASCIIEncoding AE = new ASCIIEncoding(); int tempByte = new int(); byte[] inBytes = new byte[(int)fin.Length]; encStream.Read(inBytes,0,(int)fin.Length); When it gets to the read I get the following. An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: Bad Data. Anyone have any ideas why. Everything is the same for the write. I just do not get why the data would be bad.

    J 1 Reply Last reply
    0
    • D draco_iii

      I am using the following to open and write and encryped datafile. FileStream fout = new FileStream(OptionsDataFile, FileMode.Create, FileAccess.Write); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Write); ASCIIEncoding AE = new ASCIIEncoding(); and the following to decrypt. FileStream fin = new FileStream(OptionsDataFile, FileMode.Open, FileAccess.Read); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fin, tdes.CreateDecryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Read); ASCIIEncoding AE = new ASCIIEncoding(); int tempByte = new int(); byte[] inBytes = new byte[(int)fin.Length]; encStream.Read(inBytes,0,(int)fin.Length); When it gets to the read I get the following. An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: Bad Data. Anyone have any ideas why. Everything is the same for the write. I just do not get why the data would be bad.

      J Offline
      J Offline
      Julian Bucknall MSFT
      wrote on last edited by
      #2

      Not sure what's going on for you. Maybe it's something in the code you *don't* show . The following works just fine for me (I separated out the creation of some the objects into separate statements for readability):

      class Class1 {
      static void Encrypt(string inputFile, string outputFile) {
      FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);

        FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
      
        byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
        byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104};
      
        TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); 
        ICryptoTransform t = tripleDes.CreateEncryptor(key, iv);
      
        CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
      
        int len = (int) fsin.Length;
        byte\[\] buffer = new byte\[len\];
        len = fsin.Read(buffer, 0, len);
        cs.Write(buffer, 0, len);
        
        cs.Close();
        fsout.Close();
        fsin.Close();
      }
      
      static void Decrypt(string inputFile, string outputFile) {
        FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
      
        FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
      
        byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
        byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104};
      
        TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); 
        ICryptoTransform t = tripleDes.CreateDecryptor(key, iv);
      
        CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
      
        int len = (int) fsin.Length;
        byte\[\] buffer = new byte\[len\];
        len = fsin.Read(buffer, 0, len);
        cs.Write(buffer, 0, len);
        
        cs.Close();
        fsout.Close();
        fsin.Close();
      }
      
      static void Main(string\[\] args) {
      
        Console.WriteLine("Starting encryption...");
        Encrypt(@"c:\\test.txt", @"c:\\test.enc");
        Console.WriteLine("...done");
      
        Console.WriteLine("Starting decryption...");
        Decrypt(@"c:\\test.enc", @"c:\\test.dec");
        Console.WriteLine("...done");
      
        Console.ReadLine();
      }
      

      }

      Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

      D 1 Reply Last reply
      0
      • J Julian Bucknall MSFT

        Not sure what's going on for you. Maybe it's something in the code you *don't* show . The following works just fine for me (I separated out the creation of some the objects into separate statements for readability):

        class Class1 {
        static void Encrypt(string inputFile, string outputFile) {
        FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);

          FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
        
          byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
          byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104};
        
          TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); 
          ICryptoTransform t = tripleDes.CreateEncryptor(key, iv);
        
          CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
        
          int len = (int) fsin.Length;
          byte\[\] buffer = new byte\[len\];
          len = fsin.Read(buffer, 0, len);
          cs.Write(buffer, 0, len);
          
          cs.Close();
          fsout.Close();
          fsin.Close();
        }
        
        static void Decrypt(string inputFile, string outputFile) {
          FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        
          FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
        
          byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
          byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104};
        
          TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); 
          ICryptoTransform t = tripleDes.CreateDecryptor(key, iv);
        
          CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
        
          int len = (int) fsin.Length;
          byte\[\] buffer = new byte\[len\];
          len = fsin.Read(buffer, 0, len);
          cs.Write(buffer, 0, len);
          
          cs.Close();
          fsout.Close();
          fsin.Close();
        }
        
        static void Main(string\[\] args) {
        
          Console.WriteLine("Starting encryption...");
          Encrypt(@"c:\\test.txt", @"c:\\test.enc");
          Console.WriteLine("...done");
        
          Console.WriteLine("Starting decryption...");
          Decrypt(@"c:\\test.enc", @"c:\\test.dec");
          Console.WriteLine("...done");
        
          Console.ReadLine();
        }
        

        }

        Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

        D Offline
        D Offline
        draco_iii
        wrote on last edited by
        #3

        The problem with the orginal code is I was not Seeking to the beginning of the stream.

        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