0

I'm working on a little project for fun. It's a simple application that can encrypt and decrypt files using AES in C#

Now I want to make a custom file extension for the encrypted files. This in it self is easy. But I want it to decrypt to the original file extension so it's usable again.

And when you double click on the encrypted file I want it to open the application asking for the password used to encrypt the file.

Any ideas? And feel free to correct my code.

Here is the code I use to decrypt and encrypt

class Encryption
{
    public readonly byte[] salt = new byte[] { 0x20, 0xE0, 0xdc, 0x0E, 0x00,0x70, 0x00, 0x05 };
    public const int iterations = 8192;
    public void DecryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;

        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);

        using (FileStream destination = new FileStream(destinationFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
        {
            using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
            {
                try
                {
                    using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        source.CopyTo(cryptoStream);
                    }
                }
                catch (CryptographicException exception)
                {
                    if (exception.Message == "Padding is invalid and cannot be removed.")
                        throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);
                    else
                        throw;
                }
            }
        }
    }


    public void EncryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);

        using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        {
            using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
            {
                using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.CopyTo(cryptoStream);
                }
            }
        }
    }
}

}

Rocco
  • 457
  • 5
  • 23
  • You have two questions, one being answered in [Associate File Extension with Application](http://stackoverflow.com/questions/2681878/associate-file-extension-with-application). As for the original extension, you'll need to save it somewhere. Either store it with some delimiter prepended to the actual payload, or store it in the destination filename (`output.originalextension.enc`). – CodeCaster Sep 21 '16 at 11:30
  • Well looks like I over-thinked it. Thanks for the simple solution – Rocco Sep 21 '16 at 12:34

0 Answers0