• 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
  • 相关阅读:
    在vs 2012 中使用xna 4.0 做开发!
    MVC如何分离Controller与View在不同的项目?
    Shader Compilation for Multiple Platforms
    文件已损坏,请移至废纸篓的解决办法!
    javascript学习笔记[6]面向对象
    javascript学习笔记[3]流程控制
    [tonyLp]百度地图API开发实践(1)
    javascript学习笔记[7]内建对象
    javascript学习笔记[5]函数(2)
    javascript学习笔记[1]变量
  • 原文地址:https://www.cnblogs.com/axu92312/p/14679710.html
Copyright © 2020-2023  润新知