码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-09 12:45:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/johnvwan/p/4632529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!