Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

C# Password Protected File

4.67/5 (3 votes)
14 Dec 2013CPOL2 min read 71.1K  
How to protect a file with a password (any file type) without using ZIP or any other archivers

Introduction

In this article, I'll talk about how to protect a file with a password (any file type) - without using ZIP or any other archivers. The file will be encrypted and then 'attached' to an executable, which will be later used to decrypt it using the password provided.

Starting Information

Initially, there'll be 2 files: the file we want to encrypt, and the decryptor (which is a C# executable). These files will be 'merged' together, and when needed, the decryptor will retrieve from its own file the content of the original file.

How to 'pack' the file (steps)

  1. Get the content (bytes) of the file we want to protect by password
  2. Encrypt it using a good algorithm (I'm using AES)
  3. Put everything at the end of the generic decryptor *

* the executable file won't be affected by what we add.

How to decrypt the file

  1. The file is appended at the end of the decryptor, so we need to skip as many bytes as the original decryptor's size (when it has no file attached)
  2. Load the file's content into memory -> decrypt it with the password provided
  3. Save the content as a new file

1. Creating the Decryptor

This part explains how to write the decryptor application: the resulting executable is, what I like to call, the 'original decryptor'. Its size is exactly 10240 bytes, so after we attach the encrypted file, we need to skip those bytes when we want to decrypt the content.

I made it as a Forms project only for the sake of design - anyway there's only 1 method that does all the work so it can be easily modified.

C#
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace decryptor
{
    public partial class DecryptorWnd : Form
    {
        public DecryptorWnd()
        {
            InitializeComponent();
        }

        // hardcoded size of the original decryptor...lazy coder...
        int decryptorSize = 10240;  

        private void decryptButton_Click(object sender, EventArgs e)
        {
            // sending the current process' name and the password provided by the user
            decrypt(Process.GetCurrentProcess().ProcessName, passwordTextBox.Text);
        }

        private void decrypt(string file, string password)
        {
            byte[] key = new byte[32]; // 256 bits key
            Encoding.Default.GetBytes(password).CopyTo(key, 0); // padding with 0

            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.CBC;
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.Zeros;

            using (FileStream outputStream = 
            new FileStream("decrypted_" + file, FileMode.Create))
            {
                using (CryptoStream cryptoStream = new CryptoStream
                (outputStream, aes.CreateDecryptor(key, key), CryptoStreamMode.Write))
                {

                    // reading the content of the current process
                    byte[] buffer = File.ReadAllBytes(file + ".exe");  
                   
                    // skip the original decryptor's size (we don't want to decrypt that!)
                    cryptoStream.Write(buffer, decryptorSize, buffer.Length - decryptorSize);  
                }
            }
        }
    }
}

2. Attaching the File to the Decryptor

This requires another executable file that will handle encryption and file attachment. I used a Console Application for this - to be honest, I never intended to publish this as a program. I was just testing - and for testing, the console was enough.

C#
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace FileAppender
{
    class Program
    {
        static MemoryStream mStream = new MemoryStream();

        public static void Main()
        {
            encrypt("SomeFile.txt", "myPassword");
        }

        private static void encrypt(string fileName, string password)
        {
            byte[] key = new byte[32];  // same key (256 bits)
            Encoding.Default.GetBytes(password).CopyTo(key, 0);  // padding with 0 once again

            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.CBC;
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.Zeros;

            using (CryptoStream cStream = new CryptoStream(mStream, 
            aes.CreateEncryptor(key, key), CryptoStreamMode.Write))
            {
                // reading the content of the file that requires password protection
                byte[] buffer = File.ReadAllBytes(fileName);

                // encrypting & storing everything in a MemoryStream
                cStream.Write(buffer, 0, buffer.Length);
            }

            append(fileName); // time to append
        }

        private static void append(string file)
        {
            // reading the content of the original decryptor
            byte[] exeBuffer = File.ReadAllBytes("decryptor.exe");  

            // extracting the encrypted content from the MemoryStream
            byte[] appendBuffer = mStream.ToArray() ;

            // this buffer is the 'new' decryptor, that contains the new file
            byte[] finalBuffer = new byte[exeBuffer.Length + appendBuffer.Length];

            exeBuffer.CopyTo(finalBuffer, 0);
            appendBuffer.CopyTo(finalBuffer, exeBuffer.Length);

            // creating 'SomeFile.txt.exe'
            File.WriteAllBytes(file + ".exe", finalBuffer);
        }
    }
}  

Note

This code is provided to give you an idea on how to write such a program. This means it's not optimized, so it can be improved - writing an optimized code would produce a source code harder to comprehend...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)