#region 新订购接口加解密 /// <summary> /// 对字符进行UrlEncode编码 与Java通用版 /// string转Encoding格式 /// </summary> /// <param name="text"></param> /// <param name="encod">编码格式</param> /// <param name="cap">是否输出大写字母</param> /// <returns></returns> public static string UrlEncodes(string text, Encoding encod, bool cap = true) { if (cap) { StringBuilder builder = new StringBuilder(); foreach (char c in text) { if (System.Web.HttpUtility.UrlEncode(c.ToString(), encod).Length > 1) { builder.Append(System.Web.HttpUtility.UrlEncode(c.ToString(), encod).ToUpper()); } else { builder.Append(c); } } return builder.ToString(); } else { string encodString = System.Web.HttpUtility.UrlEncode(text, encod); return encodString; } } /// <summary> /// AES加密 对应java中的 aes/cbc/pkcs5padding 模式的算法 /// </summary> /// <param name="s">待加密的字符串</param> /// <param name="sKey">密钥</param> /// <returns></returns> public static string AesEncrypt(string str, string Key,string IV) { byte[] keyArray = Encoding.UTF8.GetBytes(Key); byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.PKCS7; rDel.IV = Encoding.UTF8.GetBytes(IV); ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 对应java中的 aes/cbc/pkcs5padding 模式的算法 /// </summary> /// <param name="s">待解密的字符串</param> /// <param name="sKey">密钥</param> /// <returns>返回空为解析失败</returns> public static string AesDecrypt(string str, string key,string IV ) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(str); byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = Encoding.UTF8.GetBytes(IV); ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } public static string GenerateMD5(string txt) { using (MD5 mi = MD5.Create()) { byte[] buffer = Encoding.Default.GetBytes(txt); //开始加密 byte[] newBuffer = mi.ComputeHash(buffer); StringBuilder sb = new StringBuilder(); for (int i = 0; i < newBuffer.Length; i++) { sb.Append(newBuffer[i].ToString("x2")); } return sb.ToString(); } } #endregion