记录新项目中遇到的技术及自己忘记的技术点DES加密解密,MD5加密,字符串压缩解压,字符串截取等操作

Posted wsn1203

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录新项目中遇到的技术及自己忘记的技术点DES加密解密,MD5加密,字符串压缩解压,字符串截取等操作相关的知识,希望对你有一定的参考价值。

一、DES加密、解密

#region DES加密解密
        /// <summary>
        /// 进行DES加密
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串</param>
        /// <param name="sKey">密钥,必须为8位</param>
        /// <returns>以Base64格式返回的加密字符串</returns>
        public static string EncryptDES(string pToEncrypt, string sKey)
        {
            string key = MD5Encrypt(sKey);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(8, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                //byte[] b = ms.ToArray();
                string str = Convert.ToBase64String(ms.ToArray());//System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
                ms.Close();
                return str;
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string DecryptDES(string pToDecrypt, string sKey)
        {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);//Encoding.UTF8.GetBytes(pToDecrypt);
            string key = MD5Encrypt(sKey);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(8, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }
        #endregion

二、MD5加密(16位)

#region MD5加密
        /// <summary>
        /// 给一个字符串进行MD5加密
        /// </summary>
        /// <param name="strText">待加密字符串</param>
        /// <returns>加密后的16位字符串</returns>
        private static string MD5Encrypt(string inputStr)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(inputStr)), 4, 8);
            t2 = t2.Replace("-", "");
            t2 = t2.ToLower();
            return t2;
        }
        #endregion

三、字符串的压缩、解压

#region 字符串压缩
        /// <summary>
        /// 字符串压缩
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="str">String.</param>
        public static string CompressString(string str)
        {
            string compressString = string.Empty;
            byte[] compressBeforeByte = Encoding.UTF8.GetBytes(str);
            byte[] compressAfterByte = Compress(compressBeforeByte);
            compressString = Convert.ToBase64String(compressAfterByte);
            return compressString;
        }

        /// <summary>
        /// 字符串解压缩
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="str">String.</param>
        public static string DecompressString(string str)
        {
            string compressString = string.Empty;
            byte[] compressBeforeByte = Convert.FromBase64String(str);
            byte[] compressAfterByte = Decompress(compressBeforeByte);
            compressString = Encoding.UTF8.GetString(compressAfterByte);
            return compressString;
        }

        /// <summary>  
        /// 字节数组压缩  
        /// </summary>  
        /// <param name="strSource"></param>  
        /// <returns></returns>  
        private static byte[] Compress(byte[] data)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
                zip.Write(data, 0, data.Length);
                zip.Close();
                byte[] buffer = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, buffer.Length);
                ms.Close();
                return buffer;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>  
        /// 字节数组解压缩  
        /// </summary>  
        /// <param name="strSource"></param>  
        /// <returns></returns>  
        private static byte[] Decompress(byte[] data)
        {
            try
            {
                MemoryStream ms = new MemoryStream(data);
                GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
                MemoryStream msreader = new MemoryStream();
                byte[] buffer = new byte[0x1000];
                while (true)
                {
                    int reader = zip.Read(buffer, 0, buffer.Length);
                    if (reader <= 0)
                    {
                        break;
                    }
                    msreader.Write(buffer, 0, reader);
                }
                zip.Close();
                ms.Close();
                msreader.Position = 0;
                buffer = msreader.ToArray();
                msreader.Close();
                return buffer;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        #endregion

四、去掉

读取到的数据是“iAGAB8PHUpugMAAA==...”,在转成字符串以后不会看见,但是它还会存在,会影响后续的操作。因为我每个字符串的结尾肯定是“=”,只是不确定会有几个,所以只要从开始截取到最后一个“=”,就能得到我需要的数据。

string str = "2235=465876=68865845=2512436";
Console.WriteLine(str.Substring(0,str.LastIndexOf(=)+1));//注意+1

 

以上是关于记录新项目中遇到的技术及自己忘记的技术点DES加密解密,MD5加密,字符串压缩解压,字符串截取等操作的主要内容,如果未能解决你的问题,请参考以下文章

密码技术--对称加密算法及Go语言应用

AES和DES的比较

iOS DES,AES加密

几种常用加密算法比较

DES算法学习

详细解析DES系列加密技术