Steve Lydford on .Net

Sticking all my useful bits in one place in the hope that I may find them again one day!
posts - 4, comments - 0, trackbacks - 0

My Links

News

Hi. This just a collection of useful bits and pieces that I am posting here so that I might be able to find them again in the future. Feel free to browse through and use anything that may be of some help to you.

Archives

Post Categories

File Encryption/Decryption

Recently I needed to find a simple to way to encrypt and decrypt a file of any type (I actually needed to encrypt image and text files) and any size. I found hundreds of examples on the web, many of which just plain didn't work, or threw errors on certain file types.

Eventually I put together the following two methods using the Rijndael encryption algorithm, they simply require that you pass them the full path to the original and target files. They both require the System.Security, System.Security.Cryptography, System.Runtime.InteropServices and System.Text.RegularExpressions namespaces.

Hope they are some use to someone...

        ///<summary>
        /// Steve Lydford - 12/05/2008.
        ///
        /// Encrypts a file using Rijndael algorithm.
        ///</summary>
        ///<param name="inputFile"></param>
        ///<param name="outputFile"></param>
        private void EncryptFile(string inputFile, string outputFile)
        {
 
            try
            {
                string password = @"myKey123"; // 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");
            }
        }
        ///<summary>
        /// Steve Lydford - 12/05/2008.
        ///
        /// Decrypts a file using Rijndael algorithm.
        ///</summary>
        ///<param name="inputFile"></param>
        ///<param name="outputFile"></param>
        private void DecryptFile(string inputFile, string outputFile)
        {
 
            {
                string password = @"myKey123"; // 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();
 
            }
        }

Print | posted on Monday, May 12, 2008 10:51 PM | Filed Under [ C# ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 4 and type the answer here:

Powered by: