Home
Manage Your Code
Snippet: File Encryption Class (C#)
Title: File Encryption Class Language: C#
Description: Class to Encrypt File & File Streams Views: 129
Author: Richard Wysocki Date Added: 7/8/2008
Copy Code  
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Security.Cryptography;
5using System.IO;
6
7namespace AjaxClassLibrary
8{
9    public class FileEncryptionClass
10    {
11        #region ClassProperties   ...        #endregion ClassProperties
15        #region Private Properties   ...        #endregion Private Properties
35
36        public FileEncryptionClass()
37        {
38        }
39
40        public void EncryptionofFileStream(FileStream Source, FileStream Destination)
41        {
42            CryptoStream aCryptoStream = null;
43            try
44            {
45                RijndaelManaged RMCrypto = new RijndaelManaged();
46                aCryptoStream = new CryptoStream(Destination, RMCrypto.CreateEncryptor(EncryptionKey(), EncryptionKey()), CryptoStreamMode.Write);
47
48                int data;
49                while ((data = Source.ReadByte()) != -1)
50                    aCryptoStream.WriteByte((byte)data);
51
52            }
53            catch (Exception ex)
54            {
55                throw ex;
56            }
57            finally
58            {
59                if (aCryptoStream != null)
60                {
61                    aCryptoStream.Close();
62                }
63            }
64        } // EncryptionofFileStream

65
66        public void EncryptionofFile(string FileSource, string FileDestination)
67        {
68            if (!CheckSourceFile(FileSource))
69                return;
70
71            FileStream fsIn = new FileStream(FileSource, FileMode.Open);
72            FileStream file1 = new FileStream(FileDestination, FileMode.OpenOrCreate, FileAccess.Write);
73            try
74            {
75                EncryptionofFileStream(fsIn, file1);
76            }
77            catch (Exception ex)
78            {
79                throw ex;
80            }
81            finally
82            {
83                fsIn.Close();
84                file1.Close();
85            }
86        } // EncryptionofFile

87
88        public void DecryptionofFile(string EncryptionFileSource, string DecryptionFileSource)
89        {
90            FileStream file1 = new FileStream(EncryptionFileSource, FileMode.OpenOrCreate, FileAccess.ReadWrite);
91            FileStream fsIn = new FileStream(DecryptionFileSource, FileMode.OpenOrCreate, FileAccess.Write);
92
93            CryptoStream aCryptoStream = null;
94            try
95            {
96                RijndaelManaged RMCrypto = new RijndaelManaged();
97                aCryptoStream = new CryptoStream(file1, RMCrypto.CreateDecryptor(EncryptionKey(), EncryptionKey()), CryptoStreamMode.Read);
98
99                StreamReader reader = new StreamReader(aCryptoStream);
100
101                string data = reader.ReadToEnd();
102                byte[] data1 = ASCIIEncoding.ASCII.GetBytes(data);
103
104                fsIn.Write(data1,0,data1.Length);
105            }
106            catch (Exception ex)
107            {
108                throw ex;
109            }
110            finally
111            {
112                if (aCryptoStream != null)
113                {
114                    aCryptoStream.Close();
115                }
116                fsIn.Close();
117                file1.Close();
118            }
119
120        } // DecryptionofFile

121    } 
122}
Usage
            FileEncryptionClass EncryptFile = new FileEncryptionClass();
            EncryptFile.EncryptionofFile(@"C:\L3E1779_GatherCustomData.log", @"C:\EncryptionFile4.txt");

            EncryptFile.DecryptionofFile(@"C:\EncryptionFile4.txt", @"C:\DencryptionFile.log");

            FileStream file1 = new FileStream("C:\\EncryptionofFileSteam.txt", FileMode.OpenOrCreate, FileAccess.Write);
            FileStream fsIn = new FileStream("C:\\L3E1779_GatherCustomData.log", FileMode.OpenOrCreate);
            EncryptFile.EncryptionofFileStream(fsIn, file1);
            file1.Close();
            fsIn.Close();