記錄新項目中遇到的技術及本身忘記的技術點【DES加密解密,MD5加密,字符串壓縮、解壓,字符串截取等操做】

1、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

2、MD5加密(16位)ide

#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

3、字符串的壓縮、解壓加密

#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

4、去掉\0spa

讀取到的數據是「iAGAB8PHUpugMAAA==\0\0\0\0\0\0...\0\0」,在轉成字符串之後不會看見\0,可是它還會存在,會影響後續的操做。由於我每一個字符串的結尾確定是「=」,只是不肯定會有幾個,因此只要從開始截取到最後一個「=」,就能獲得我須要的數據。code

string str = "2235=465876=68865845=2512436"; Console.WriteLine(str.Substring(0,str.LastIndexOf('=')+1));//注意+1
相關文章
相關標籤/搜索