东哥的博客

我的学习笔记!

导航

字符串的加密与解密(二)

Posted on 2015-07-23 22:32  Donge  阅读(362)  评论(0)    收藏  举报

二、可逆加密

以下的几种加密和解密均要添加对System.Security.Cryptography命名空间的引用;

using System.Security.Cryptography;

1、DES

    public class DESDemo
    {
        /// <summary>
        /// 向量
        /// 向量的长度为8位,也就是DES算法的块大小,经本人亲测,若小于8位程序会抛出异常,
        /// 若大于8位,则8位以后的不起作用。
        /// 字节数组里的值可以根据个人需要进行更改
        /// 这个参数也可使用以下方式初始化:
        /// private static byte[] rgbIV = Encoding.ASCII.GetBytes("!@#abc12");
        /// </summary>
        private static byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// 使用DES算法加密字符串
        /// </summary>
        /// <param name="strEncrypt">待加密的字符串</param>
        /// <param name="key">密钥,与向量一样必须为8位长度,否则会抛出异常</param>
        /// <returns>加密后的字符串(Base64编码格式)</returns>
        public static string DESEncypt(string strEncrypt, string key)
        {
            if (key.Length != 8)
            {
                throw new Exception("密钥的长度必须为8!");
            }
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            string result = "";
            try
            {
                byte[] rgbKey = Encoding.ASCII.GetBytes(key);
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strEncrypt);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                result = Convert.ToBase64String(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally
            {
                des.Clear();
            }
            return result;
        }

        /// <summary>
        /// 使用DES算法解密字符串
        /// </summary>
        /// <param name="strDecrypt">待解密的字符串</param>
        /// <param name="key">密钥,与加密时一致</param>
        /// <returns>解密后的字符串</returns>
        public static string DESDecrypt(string strDecrypt, string key)
        {
            if (key.Length != 8)
            {
                throw new Exception("密钥的长度必须为8!");
            }
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            string result = "";
            try
            {
                byte[] rgbKey = Encoding.ASCII.GetBytes(key);
                byte[] inputBytesArray = Convert.FromBase64String(strDecrypt);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputBytesArray, 0, inputBytesArray.Length);
                cStream.FlushFinalBlock();
                result = Encoding.UTF8.GetString(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally
            {
                des.Clear();
            }
            return result;
        }
    }

调用方法:

        static void Main(string[] args)
        {
            Console.WriteLine("请输入要加密的字符串:");
            string s = Console.ReadLine();
            string key = "DeS*#06#";
            string result = DESDemo.DESEncypt(s, key);
            Console.WriteLine("密钥为:{0}", key);
            Console.WriteLine("DES加密后:{0}", result);
            Console.WriteLine("DES解密后:{0}", DESDemo.DESDecrypt(result, key));
            Console.ReadKey();
        }

运行结果如下:

image

2、RC2

public class RC2Demo
    {
        /// <summary>
        /// 密钥向量,可修改
        /// 经测试,密钥的向量的长度必须为8位(传说此算法的块大小),若小于8位则会
        /// 报错,若大于8位8位以后的不起作用
        /// </summary>
        private static byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
        /// <summary>
        /// 使用RC2加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">密钥(长度必须在5-16之间)</param>
        /// <returns></returns>
        public static string RC2Encrypt(string encryptString, string encryptKey)
        {
            if (encryptKey.Length < 5 || encryptKey.Length > 16)
                throw new Exception("密钥长度必须在5-16之间");
            string m_strEncrypt = "";
            byte[] m_Key=Encoding.UTF8.GetBytes(encryptKey);
            RC2CryptoServiceProvider rCSP = new RC2CryptoServiceProvider();
            try
            {
                byte[] m_btEncryptString = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, rCSP.CreateEncryptor(m_Key, m_btIV), CryptoStreamMode.Write);
                cStream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
                cStream.FlushFinalBlock();
                m_strEncrypt = Convert.ToBase64String(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { rCSP.Clear(); }

            return m_strEncrypt;
        }

        /// <summary>
        /// 使用RC2解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">密钥(长度必须在5-16位之间)</param>
        /// <returns></returns>
        public static string RC2Decrypt(string decryptString, string decryptKey)
        {
            if (decryptKey.Length < 5 || decryptKey.Length > 16)
                throw new Exception("密钥的长度必须在5-16位之间");
            string m_decryptString = "";
            RC2CryptoServiceProvider rCSP = new RC2CryptoServiceProvider();
            try
            {
                byte[] m_rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] m_inputString = Convert.FromBase64String(decryptString);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, rCSP.CreateDecryptor(m_rgbKey, m_btIV), CryptoStreamMode.Write);
                cStream.Write(m_inputString, 0, m_inputString.Length);
                cStream.FlushFinalBlock();
                m_decryptString = Encoding.UTF8.GetString(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { rCSP.Clear(); }

            return m_decryptString;
        }
    }

3、AES

public class AESDemo
    {
        /// <summary>
        /// 初始化向量,可修改
        /// 长度必须为16位,小于16位会报错,大于16位则16位后的不起作用
        /// 可以是下列三个方式的任意一种
        /// </summary>
        //private static byte[] m_rgbIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
        //private static byte[] m_rgbIV = Encoding.UTF8.GetBytes("123456789012345");
        private static byte[] m_rgbIV = {0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,0x90,0x0A,0xAB,0xBC,0xCD,0xDE,0xEF,0xFF };
        /// <summary>
        /// 使用AES加密
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">密钥(长度最小为8位)</param>
        /// <returns></returns>
        public static string AESEncrypt(string encryptString, string encryptKey)
        {
            string m_Strencrypt = "";
            Rijndael aes = Rijndael.Create();
            //也可以使用下列方式
            //AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            try
            {
                byte[] input = Encoding.UTF8.GetBytes(encryptString);
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(rgbKey, m_rgbIV), CryptoStreamMode.Write);
                cStream.Write(input, 0, input.Length);
                cStream.FlushFinalBlock();
                m_Strencrypt = Convert.ToBase64String(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { aes.Clear(); }

            return m_Strencrypt;
        }

        public static string AESDecrypt(string decryptString, string descryptKey)
        {
            string m_strDecrypt = "";
            Rijndael aes = Rijndael.Create();
            try
            {
                byte[] input = Convert.FromBase64String(decryptString);
                byte[] rgbKey = Encoding.UTF8.GetBytes(descryptKey);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(rgbKey, m_rgbIV), CryptoStreamMode.Write);
                cStream.Write(input, 0, input.Length);
                cStream.FlushFinalBlock();
                m_strDecrypt = Encoding.UTF8.GetString(mStream.ToArray());
                mStream.Close();
                mStream.Dispose();
                cStream.Close();
                cStream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex;}
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { aes.Clear(); }

            return m_strDecrypt;
        }
    }