The 'factoryised' version of the cryptography class I mentioned previously...

I posted earlier about the really useful little encryption class by Ian Medvedev and mentioned that this could be factoryised pretty easy (well not really factoryised but you can switch the encryption provider). Anyway, here's what I can up with, pretty simple extension of the original allowing you to use whichever encryption type you want.

using System;

using System.IO;

using System.Security.Cryptography;

 

namespace SerializableJob.Compression

{

            public enum EncryptionType

            {

                        DES,

                        RC2,

                        TripleDES,

                        Rijndael

            }

            public class Encryption

            {

                        public static EncryptionType Algorithm = EncryptionType.Rijndael;

                        private static SymmetricAlgorithm m_cAlg

                        {

                                    get

                                    {

                                                switch(Algorithm)

                                                {

                                                            case(EncryptionType.Rijndael):

                                                                        return Rijndael.Create();

                                                            case(EncryptionType.DES):

                                                                        return DES.Create();

                                                            case(EncryptionType.RC2):

                                                                        return RC2.Create();

                                                            case(EncryptionType.TripleDES):

                                                                        return TripleDES.Create();

                                                            default:

                                                                        return Rijndael.Create();

                                                }         

                                    }

            }

 

                        public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)

                        {

                                    // Create a MemoryStream that is going to accept the encrypted bytes

                                    MemoryStream ms = new MemoryStream();

                                    SymmetricAlgorithm alg = m_cAlg;

                                    alg.Key = Key;

                                    alg.IV = IV;

                                    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

                                    cs.Write(clearData, 0, clearData.Length);

                                    cs.Close();

                                    byte[] encryptedData = ms.ToArray();

                                    return encryptedData;

                        }

 

                        public static string Encrypt(string clearText, string Password)

                        {

                                    byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));

                                    return Convert.ToBase64String(encryptedData);

                        }

 

                        public static byte[] Encrypt(byte[] clearData, string Password)

                        {

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));

                        }

 

                        public static void Encrypt(string fileIn, string fileOut, string Password)

                        {

                                    FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read);

                                    FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    SymmetricAlgorithm alg = m_cAlg;

                                    alg.Key = pdb.GetBytes(32);

                                    alg.IV = pdb.GetBytes(16);

                                    CryptoStream cs = new CryptoStream(fsOut, alg.CreateEncryptor(), CryptoStreamMode.Write);

                                    int bufferLen = 4096;

                                    byte[] buffer = new byte[bufferLen];

                                    int bytesRead;

                                    do

                                    {

                                                bytesRead = fsIn.Read(buffer, 0, bufferLen);

                                                cs.Write(buffer, 0, bytesRead);

                                    } while(bytesRead != 0);

                                    cs.Close();

                                    fsIn.Close();    

                        }

 

                        // Decrypt a byte array into a byte array using a key and an IV

                        public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)

                        {

                                    MemoryStream ms = new MemoryStream();

                                    SymmetricAlgorithm alg = m_cAlg;

                                    alg.Key = Key;

                                    alg.IV = IV;

                                    CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

                                    cs.Write(cipherData, 0, cipherData.Length);

                                    cs.Close();

                                    byte[] decryptedData = ms.ToArray();

                                    return decryptedData;

                        }

 

                        public static string Decrypt(string cipherText, string Password)

                        {

                                    byte[] cipherBytes = Convert.FromBase64String(cipherText);

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));

                                    return System.Text.Encoding.Unicode.GetString(decryptedData);

                        }

 

                        public static byte[] Decrypt(byte[] cipherData, string Password)

                        {

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    return Decrypt(cipherData, pdb.GetBytes(32), pdb.GetBytes(16));

                        }

 

                        public static void Decrypt(string fileIn, string fileOut, string Password)

                        {

                                    FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read);

                                    FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);

                                    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

                                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                                    SymmetricAlgorithm alg = m_cAlg;

                                    alg.Key = pdb.GetBytes(32);

                                    alg.IV = pdb.GetBytes(16);

                                    CryptoStream cs = new CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write);

                                    int bufferLen = 4096;

                                    byte[] buffer = new byte[bufferLen];

                                    int bytesRead;

                                    do

                                    {

                                                bytesRead = fsIn.Read(buffer, 0, bufferLen);

                                                cs.Write(buffer, 0, bytesRead);

                                    } while(bytesRead != 0);

                                    cs.Close();

                                    fsIn.Close();    

                        }

            }

}

Print | posted @ Monday, April 05, 2004 1:47 PM

Comments on this entry:

No comments posted yet.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 3 and 4 and type the answer here: