C# AES要解密的数据的长度无效

加密方式  AES-CBC-128

将解密方法改成如下

public  string Decrypt(string toDecrypt, string key)
{
            if (string.IsNullOrEmpty(toDecrypt)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                IV=Encoding.UTF8.GetBytes(key),
                Mode = System.Security.Cryptography.CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

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

            return Encoding.UTF8.GetString(resultArray);
}

 

posted @ 2018-01-12 18:39  韩梦芫  阅读(5239)  评论(0编辑  收藏  举报