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. [SOLVED] File Encryption Broken [modified]

[SOLVED] File Encryption Broken [modified]

Scheduled Pinned Locked Moved C#
securityhelp
2 Posts 1 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.
  • B Offline
    B Offline
    Ben Magee
    wrote on last edited by
    #1

    Hello, I Am using the following functions to encrypt and decrypt files: Encrypt:

        private void EncryptFile(string inputFile, string outputFile)
        {
    
            try
            {
                string password = @"19651969"; // Your Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte\[\] key = UE.GetBytes(password);
    
                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
    
                RijndaelManaged RMCrypto = new RijndaelManaged();
    
                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateEncryptor(key, key),
                    CryptoStreamMode.Write);
    
                FileStream fsIn = new FileStream(inputFile, FileMode.Open);
    
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);
    
    
                fsIn.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch
            {
                MessageBox.Show("Encryption failed!", "Error");
            }
        }
    

    Decrypt:

        private void DecryptFile(string inputFile, string outputFile)
        {
    
            {
                string password = @"19651969"; // Your Key Here
    
                UnicodeEncoding UE = new UnicodeEncoding();
                byte\[\] key = UE.GetBytes(password);
    
                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
    
                RijndaelManaged RMCrypto = new RijndaelManaged();
    
                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);
    
                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
    
                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);
    
                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
    
            }
        }
    

    I'm using the following code to Decrypt my encrypted file, read it, then encrypt it again.

        private void Form1\_Load(object sender, EventArgs e)
        {       
            
            if (File.Exists("scf.sf"))
            {
                DecryptFile("scf.sf", "sff.sf");
                File.Delete("scf.sf");
                File.Move("sff.sf", "scf.sf");
    
    B 1 Reply Last reply
    0
    • B Ben Magee

      Hello, I Am using the following functions to encrypt and decrypt files: Encrypt:

          private void EncryptFile(string inputFile, string outputFile)
          {
      
              try
              {
                  string password = @"19651969"; // Your Key Here
                  UnicodeEncoding UE = new UnicodeEncoding();
                  byte\[\] key = UE.GetBytes(password);
      
                  string cryptFile = outputFile;
                  FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
      
                  RijndaelManaged RMCrypto = new RijndaelManaged();
      
                  CryptoStream cs = new CryptoStream(fsCrypt,
                      RMCrypto.CreateEncryptor(key, key),
                      CryptoStreamMode.Write);
      
                  FileStream fsIn = new FileStream(inputFile, FileMode.Open);
      
                  int data;
                  while ((data = fsIn.ReadByte()) != -1)
                      cs.WriteByte((byte)data);
      
      
                  fsIn.Close();
                  cs.Close();
                  fsCrypt.Close();
              }
              catch
              {
                  MessageBox.Show("Encryption failed!", "Error");
              }
          }
      

      Decrypt:

          private void DecryptFile(string inputFile, string outputFile)
          {
      
              {
                  string password = @"19651969"; // Your Key Here
      
                  UnicodeEncoding UE = new UnicodeEncoding();
                  byte\[\] key = UE.GetBytes(password);
      
                  FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
      
                  RijndaelManaged RMCrypto = new RijndaelManaged();
      
                  CryptoStream cs = new CryptoStream(fsCrypt,
                      RMCrypto.CreateDecryptor(key, key),
                      CryptoStreamMode.Read);
      
                  FileStream fsOut = new FileStream(outputFile, FileMode.Create);
      
                  int data;
                  while ((data = cs.ReadByte()) != -1)
                      fsOut.WriteByte((byte)data);
      
                  fsOut.Close();
                  cs.Close();
                  fsCrypt.Close();
      
              }
          }
      

      I'm using the following code to Decrypt my encrypted file, read it, then encrypt it again.

          private void Form1\_Load(object sender, EventArgs e)
          {       
              
              if (File.Exists("scf.sf"))
              {
                  DecryptFile("scf.sf", "sff.sf");
                  File.Delete("scf.sf");
                  File.Move("sff.sf", "scf.sf");
      
      B Offline
      B Offline
      Ben Magee
      wrote on last edited by
      #2

      Oops, My bad, i'd replaced the file so many times in testing I forgot to encrpypt it again :^)

      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