zoukankan      html  css  js  c++  java
  • 记录新项目中遇到的技术及自己忘记的技术点【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
  • 相关阅读:
    JavaScript:综合案例---房贷计算器的实现
    iOS:如何将自己的SDK用CocoaPods管理
    JavaScript:综合案例-表单验证
    JavaScript:日期选择器组件的使用
    JavaScript : 基本的处理事件
    JavaScript:window窗口对象
    JavaScript:文本域事件处理
    JavaScript:下拉列表框的事件处理
    JavaScript:复选框事件的处理
    JavaScript:单选钮的事件处理
  • 原文地址:https://www.cnblogs.com/wsn1203/p/9341207.html
Copyright © 2011-2022 走看看