AES加密解密算法——使用.NET中Rijndael实现

public sealed class RijndaelEncDec

    {

        /// <summary>

        /// 初始化加密的key

        /// </summary>

        public static string Password

        {

            get;set;

        }



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

        {

            MemoryStream ms = new MemoryStream();



            Rijndael alg = Rijndael.Create();



            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;

        }



        /// <summary>

        /// 返回通过key加密后的字符串

        /// </summary>

        /// <param name="clearText"></param>

        /// <returns></returns>

        public static string Encrypt(string clearText)

        {

            return Encrypt(clearText, Password);

        }



        private static string Encrypt(string clearText, string password)

        {

            byte[] clearBytes =

                System.Text.Encoding.Unicode.GetBytes(clearText);



            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(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);



        }



        private static byte[] Encrypt(byte[] clearData)

        {

            return Encrypt(clearData, Password);

        }



        private static byte[] Encrypt(byte[] clearData, string password)

        {

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(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));



        }



        /// <summary>

        /// 导出加密后的文件

        /// </summary>

        /// <param name="fileIn">需要加密的文本</param>

        /// <param name="fileOut">加密后的文本</param>

        /// <param name="foo">预留标记未做使用,默认传入true即可</param>

        public static void Encrypt(string fileIn, string fileOut, bool foo)

        {

            Encrypt(fileIn, fileOut, Password);

        }



        private 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);



            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,

                new byte[]

                {

                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,

                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76

                });



            Rijndael alg = Rijndael.Create();

            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();

        }



        private static byte[] Decrypt(byte[] cipherData,

            byte[] Key, byte[] IV)

        {

            MemoryStream ms = new MemoryStream();



            Rijndael alg = Rijndael.Create();



            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;

        }



        /// <summary>

        /// 返回通过key解密的原始字符串

        /// </summary>

        /// <param name="cipherText"></param>

        /// <returns></returns>

        public static string Decrypt(string cipherText)

        {

            return Decrypt(cipherText, Password);

        }



        private static string Decrypt(string cipherText, string password)

        {

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



            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(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);

        }





        private static byte[] Decrypt(byte[] cipherData, string password)

        {

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(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));

        }



        /// <summary>

        /// 返回解密后的文本

        /// </summary>

        /// <param name="fileIn"></param>

        /// <param name="fileOut"></param>

        /// <param name="foo">预留标记未做使用,默认传入true即可</param>

        public static void Decrypt(string fileIn, string fileOut, bool foo)

        {

            Decrypt(fileIn, fileOut, Password);

        }



        private 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);



            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,

                new byte[]

                {

                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,

                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76

                });

            Rijndael alg = Rijndael.Create();



            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();

        }

    }

 

你可能感兴趣的:(.net)