zoukankan      html  css  js  c++  java
  • c# aes/cbc/pkcs5padding 模式的算法

    #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
  • 相关阅读:
    Visual Studio 中创建带有向导的项目模板
    通过.NET Remoting调用CCNET接口的方法
    django单元测试历险记
    我的测试生活感悟3 淘宝的接口测试白皮书
    期待《 Beautiful Testing 》( 测试之美 )
    我的测试生活感悟4 谈谈面试
    NancyBlog 我的Google App Engine Blog
    我的测试生活感悟1
    我的测试生活感悟2 Art Of Unit Testing
    《微软的软件测试之道》(How We Test Software at Microsoft)
  • 原文地址:https://www.cnblogs.com/axu92312/p/14679710.html
Copyright © 2011-2022 走看看