码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-23 23:41:41      阅读:195      评论:0      收藏: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>加密后的字符串</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();
        }

运行结果如下:

技术分享

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

标签:

原文地址:http://www.cnblogs.com/Donge/p/4671982.html

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