u014510977
尛丶MaTure
2017-03-20 16:25

关于C# AES加密的PaddingMode的问题

20
  • c#
  • 加密
  • aes
    public static string AesEncrypt(string str, string key, string iv)
    {
        if (string.IsNullOrEmpty(str)) return null;

        Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
        byte[] keyArr = Encoding.UTF8.GetBytes(key);
        byte[] ivArr = Encoding.UTF8.GetBytes(iv);

        System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();

        /***************************这里有问题*****************************/
        rm.Padding = System.Security.Cryptography.PaddingMode.None;
        /******************************************************************/

        rm.Mode = System.Security.Cryptography.CipherMode.CFB;
        rm.BlockSize = 128;

        System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(keyArr, ivArr);

        Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

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

方法调用

String str = "appid=pm93c050eb69884a50&appsecret=b24ee0ef-73be-4a9a-a884-e0c0e27c&timestamp=1489732369";
string aa = AesEncrypt(str, "dha@wjw$pvms9wwl", "8807599889957088");

PaddingMode设置成PaddingMode.None 不填充时 总会报错

有人知道这个问题怎么解决吗


```![图片说明](https://img-ask.csdn.net/upload/201703/21/1490027098_177459.png)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答