C#Aes CryptoStream指定的填充模式对于.NET CORE 2.0中的此算法无效

时间:2018-02-07 10:36:17

标签: c# encryption .net-core aes

您好StackOverflow社区, 我无法弄清楚它有什么问题。 任何人都可以解释它并告诉我如何修复bug。 非常感谢!

下面的代码给了我一个System.Security.Cryptography.CryptographicException:'指定的填充模式对此算法无效。'

static void Main(string[] args)
        {

            var enCrypto= Encyrpt("AllenLi","qwefdssdf");
            var deCrypto=lDecyrpt(enCrypto,"qwefdssdf");

            System.Console.WriteLine(deCrypto);
        }
        private static readonly byte[] salt=Encoding.Unicode.GetBytes("salts@@");
        public static string Encyrpt(string plainText,string password){
            byte[] plainBytes=Encoding.Unicode.GetBytes(plainText);
            var aes=Aes.Create();

            //generating keys ,IV
            var pbkdf2=new Rfc2898DeriveBytes(password,salt,2000);
            var aesKey=pbkdf2.GetBytes(32);
            var aesIV=pbkdf2.GetBytes(16);
            var ms =new MemoryStream();
            using(var cs=new CryptoStream(ms,aes.CreateEncryptor(),CryptoStreamMode.Write)){
                    cs.Write(plainBytes,0,plainBytes.Length);

            };
            return Convert.ToBase64String(ms.ToArray());

        }
        public static string lDecyrpt(string cryptoText,string password)
        {
            byte[] cryptoBytes=Convert.FromBase64String(cryptoText);
            var aes=Aes.Create();

            //generating keys ,IV
            var pbkdf2=new Rfc2898DeriveBytes(password,salt,2000);
            var aesKey=pbkdf2.GetBytes(32);
            var aesIV=pbkdf2.GetBytes(16);
            var ms =new MemoryStream();
            using(var cs=new CryptoStream(ms,aes.CreateDecryptor(),CryptoStreamMode.Write)){


                cs.Write(cryptoBytes,0,cryptoBytes.Length);
            };
            return Encoding.Unicode.GetString(ms.ToArray());

        }


exception
Unhandled Exception: System.Security.Cryptography.CryptographicException: Specified padding mode isnot valid for this algorithm.
   at Internal.Cryptography.UniversalCryptoDecryptor.DepadBlock(Byte[] block, Int32 offset, Int32 count)
   at Internal.Cryptography.UniversalCryptoDecryptor.UncheckedTransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at Internal.Cryptography.UniversalCryptoTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
   at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Ch11_Encyrption.Program.lDecyrpt(String cryptoText, String password) in /Users/Li/Documents/Code/Ch11/Ch11_Encyrption/Program.cs:line 51
   at Ch11_Encyrption.Program.Main(String[] args) in /Users/Li/Documents/Code/Ch11/Ch11_Encyrption/Program.cs:line 16

1 个答案:

答案 0 :(得分:1)

您没有在aes对象上设置Key和IV。见下文:

static void Main(string[] args)
        {

            var enCrypto= Encyrpt("AllenLi","qwefdssdf");
            var deCrypto=lDecyrpt(enCrypto,"qwefdssdf");

            System.Console.WriteLine(deCrypto);
        }
        private static readonly byte[] salt=Encoding.Unicode.GetBytes("salts@@");
        public static string Encyrpt(string plainText,string password){
            byte[] plainBytes=Encoding.Unicode.GetBytes(plainText);
            var aes=Aes.Create();

            //generating keys ,IV
            var pbkdf2=new Rfc2898DeriveBytes(password,salt,2000);
            var aesKey=pbkdf2.GetBytes(32);
            var aesIV=pbkdf2.GetBytes(16);

            aes.Key = aesKey;
            aes.IV = aesIV;

            var ms =new MemoryStream();
            using(var cs=new CryptoStream(ms,aes.CreateEncryptor(),CryptoStreamMode.Write)){
                    cs.Write(plainBytes,0,plainBytes.Length);

            };
            return Convert.ToBase64String(ms.ToArray());

        }
        public static string lDecyrpt(string cryptoText,string password)
        {
            byte[] cryptoBytes=Convert.FromBase64String(cryptoText);
            var aes=Aes.Create();

            //generating keys ,IV
            var pbkdf2=new Rfc2898DeriveBytes(password,salt,2000);
            var aesKey=pbkdf2.GetBytes(32);
            var aesIV=pbkdf2.GetBytes(16);

            aes.Key = aesKey;
            aes.IV = aesIV;

            var ms =new MemoryStream();
            using(var cs=new CryptoStream(ms,aes.CreateDecryptor(),CryptoStreamMode.Write)){


                cs.Write(cryptoBytes,0,cryptoBytes.Length);
            };
            return Encoding.Unicode.GetString(ms.ToArray());

        }