【问题标题】:C# Aes CryptoStream Specified padding mode is not valid for this algorithm in .NET CORE 2.0 [duplicate]C# Aes CryptoStream 指定的填充模式对于 .NET CORE 2.0 中的此算法无效 [重复]
【发布时间】:2018-02-07 10:36:17
【问题描述】:

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

下面的代码给了我一个 System.Security.Cryptography.CryptographicException: 'Specified padding mode is not valid for this algorithm。'

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

【问题讨论】:

  • @mjwills 嗨,我附上有关异常的信息。
  • 哪一行抛出异常?
  • 在代码“cs.Write(cryptoBytes,0,cryptoBytes.Length);”之后

标签: c# encryption .net-core aes


【解决方案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());

        }

【讨论】:

  • 感谢指出问题是由于我的粗心造成的。 :)
猜你喜欢
  • 2018-06-23
  • 2018-03-13
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多