Click here to Skip to main content
14,772,885 members
Articles » Desktop Development » Files and Folders » Encryption
Posted 14 May 2008


86 bookmarked

File Encryption and Decryption in C#

Rate me:
Please Sign up or sign in to vote.
4.18/5 (40 votes)
14 May 2008CPOL
Demonstrates how to encrypt and decrypt any file type using C#.


This article demonstrates how to use C# to encrypt and decrypt files of any type.


Recently, I needed to find a simple 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 use of the System.Security, System.Security.Cryptography, System.Runtime.InteropServices, and System.Text.RegularExpressions namespaces.

Unfortunately, as I looked at so many examples on the web and actually did all this a while ago, I do not remember which bits of code were originally written by who. So, if you recognise some of it, many thanks, leave a comment below so that your work doesn't go unrecognised.

Using the code

There are two methods: encryptFile and decryptFile. They both require that you pass in the filenames and paths of the source and destination files as strings. It is important that the user has the necessary file rights to create the encrypted file.

/// Steve Lydford - 12/05/2008.
/// Encrypts a file using Rijndael algorithm.
///<param name="inputFile"></param>
///<param name="outputFile"></param>
private void EncryptFile(string inputFile, string outputFile)

        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),

        FileStream fsIn = new FileStream(inputFile, FileMode.Open);

        int data;
        while ((data = fsIn.ReadByte()) != -1)

        MessageBox.Show("Encryption failed!", "Error");
/// Steve Lydford - 12/05/2008.
/// Decrypts a file using Rijndael algorithm.
///<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),

        FileStream fsOut = new FileStream(outputFile, FileMode.Create);

        int data;
        while ((data = cs.ReadByte()) != -1)




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


About the Author

Steve Lydford
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionGetting Error in PDF Files Pin
Jayamanickam30-Dec-19 22:02
MemberJayamanickam30-Dec-19 22:02 
GeneralVery useful article Pin
Member 1384626017-Jul-19 10:42
MemberMember 1384626017-Jul-19 10:42 
QuestionThanks Pin
Daniel Macias23-Apr-18 7:02
MemberDaniel Macias23-Apr-18 7:02 
QuestionSystem Exception on encryption Pin
Wilbur.S5-Dec-17 14:39
MemberWilbur.S5-Dec-17 14:39 
QuestionFile Encryption and Decryption in C# Pin
Member 1053416216-Sep-15 4:00
MemberMember 1053416216-Sep-15 4:00 
QuestionUsing the some filepath Pin
Member 114398482-Jun-15 10:42
MemberMember 114398482-Jun-15 10:42 
AnswerRe: Using the some filepath Pin
@cromx15-Jun-15 20:32
Member@cromx15-Jun-15 20:32 
QuestionDecryption problem Pin
Member 1143778215-Mar-15 22:29
MemberMember 1143778215-Mar-15 22:29 
GeneralMy vote of 5 Pin
mehdi12112-Mar-15 0:26
Membermehdi12112-Mar-15 0:26 
QuestionProblem with DecryptFile Pin
m.b.v27-Nov-14 4:31
Memberm.b.v27-Nov-14 4:31 
QuestionNice Pin
ezelden.sahi10-Apr-14 1:39
Memberezelden.sahi10-Apr-14 1:39 
QuestionProvide me the complete application so I can run that Pin
Member 106801324-Apr-14 1:01
MemberMember 106801324-Apr-14 1:01 
GeneralWOW!!!!! MY VOTE OF 5.. Pin
parkavikarthi21-Sep-13 2:22
Memberparkavikarthi21-Sep-13 2:22 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi29-Jul-13 23:19
professionalAmir Mohammad Nasrollahi29-Jul-13 23:19 
GeneralMy vote of 5 Pin
JPFriederich19-Jun-13 1:49
MemberJPFriederich19-Jun-13 1:49 
GeneralMy vote of 3 Pin
B.Farivar27-Apr-13 20:40
MemberB.Farivar27-Apr-13 20:40 
QuestionEncrypion/Decryption Pin
Peter Van Zyl11-Feb-13 3:40
MemberPeter Van Zyl11-Feb-13 3:40 
GeneralThanks Pin
Imteyaz Ahmed3-Sep-12 9:14
MemberImteyaz Ahmed3-Sep-12 9:14 
GeneralRe: Thanks Pin
Steve Lydford4-Sep-12 6:01
MemberSteve Lydford4-Sep-12 6:01 
QuestionInput and Output files Pin
peek4y1-Jul-12 21:58
Memberpeek4y1-Jul-12 21:58 
GeneralMy vote of 4 Pin
kaliprasad12317-Jun-12 22:05
Memberkaliprasad12317-Jun-12 22:05 
GeneralEncryption Pin
Freshino14-May-12 22:56
MemberFreshino14-May-12 22:56 
QuestionError when Encrypting Pin
Member 795084531-Oct-11 5:09
MemberMember 795084531-Oct-11 5:09 
Generalexception [modified] Pin
samson radu11-May-11 4:54
Membersamson radu11-May-11 4:54 
Questionkey length Pin
ale.zad23-Apr-11 4:11
Memberale.zad23-Apr-11 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.