DES加密深度优化——随机生成加密字符串

DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法。但该算法一旦密钥固定,加密的字符串也就随之固定,这不利于数据存储安全。而且用该方法加密后有一个显著的标记,末位几位为‘=’。所有本次优化对密钥和去等进行了一些优化。

首先我们生成8位随机字符串作为加密的key值。

  private static string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";

        /// <summary>
        /// 生成内容随机key
        /// </summary>
        /// <returns></returns>
        public static string GetContent()
        {
            Random rand = new Random();
            string str = null;
            for (int i = 0; i < 8; i++)
            {
                str += so.Substring(rand.Next(62), 1);
            }
            return str;
        }

然后将key值进行拆分为两部分。

   Random rand = new Random();
                int r = rand.Next(9);
                string encryptKey = GetContent();
                string first = encryptKey.Substring(0, r);
                string last = encryptKey.Substring(r);

最后对需加密的字符串进行DES加密,并记录等号的个数并截取去除等号字符串,然后与key值左右部分拼接。

    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary> 
        /// DES加密字符串 
        /// </summary> 
        /// <param name="encryptString">待加密的字符串</param> 
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 
        public static string Encrypt(string encryptString)
        {
            try
            {
                Random rand = new Random();
                int r = rand.Next(9);
                string encryptKey = GetContent();
                string first = encryptKey.Substring(0, r);
                string last = encryptKey.Substring(r);

                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCsp = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                string str = Convert.ToBase64String(mStream.ToArray());
                int i = 0;
                for (int j = str.Length - 1; j > 0; j--)
                {
                    if (str[j] != '=')
                    {
                        break;
                    }
                    else
                    {
                        i++;
                    }
                }
                str = str.Substring(0, str.Length - i);
                return i.ToString()+r.ToString()+first + str +last;
            }
            catch
            {
                return encryptString;
            }
        }

对加密的字符串进行解密。

    /// <summary> 
        /// DES解密字符串 
        /// </summary> 
        /// <param name="decryptString">待解密的字符串</param> 
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 
        public static string Decrypt(string decryptString)
        {
            try
            {
                int code = Int32.Parse(decryptString.Substring(1, 1));
                int i = Int32.Parse(decryptString.Substring(0, 1));

                string first = decryptString.Substring(2, code);
                string last = decryptString.Substring(decryptString.Length - 8 + code);
                string encryptKey =first+last;
                decryptString = decryptString.Substring(code + 2, decryptString.Length - 10);
                for (int j = 0; j < i; j++)
                {
                    decryptString += "=";
                }
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dcsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

应用及演示:

   public static void Main(string[] args)
        {
            string str = "xuhang";
            for (int i = 0; i < 10; i++)
            {
                var m = DesNew.Encrypt(str);
                Console.WriteLine(m);
                Console.WriteLine(DesNew.Decrypt(m));
                Console.WriteLine("------------------------------");
                Thread.Sleep(500); //0.5s后唤醒线程,避免由于时间短生成的数据一致
            }
        }

原文地址:https://www.cnblogs.com/xuhang/p/4205757.html