分润宝,专注各种代理商分润系统开发!完美兼容各支付公司产品代理商分润结算模式。
全国服务热线0592-5519259 在线咨询 微信咨询

分润宝 手机端

在线客服

常见问题 > 技术文档

AES加密和解密

 /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="str">要加密字符串</param>
        /// <returns>返回加密后字符串</returns>
        public static String Encrypt_AES(String str, string strAesKey)
        {
            //string str1 = Server.UrlEncode(str);
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
            Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            System.Text.Encoding encoding = System.Text.Encoding.Default;
            string eurl = HttpUtility.UrlEncode(resultArray.ToString());
            eurl = Convert.ToBase64String(encoding.GetBytes(eurl));

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);

            //Encoding encoding = Encoding.GetEncoding("UTF-8");
            //byte[] bytes = encoding.GetBytes(resultArray.ToString()));
            //return Convert.ToBase64String(bytes);

        }

/// <summary>
        /// AES解密
        /// </summary>
        /// <param name="str">要解密字符串</param>
        /// <returns>返回解密后字符串</returns>
        public static String Decrypt_AES(String str, string strAesKey)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
        }

如果没有解决您的问题,请立即联系客服

声明:文章"AES加密和解密"为分润宝原创文章,转载请注明出处,谢谢合作!

分润宝公众号

客服

微信客服

置顶