显示错误填充无效,无法删除. [英] showing error Padding is invalid and cannot be removed.

查看:76
本文介绍了显示错误填充无效,无法删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 iam在基于Windows的application.im中尝试尝试解密加密密码.我可以很好地加密密码,但是我尝试显示这样的decrpt错误

填充无效,无法删除

我使用下面的代码

Hi iam working in windows based application.im trying to decrpt an encrypt password .i can encrypt passwords well but im trying decrpt error showing like this

Padding is invalid and cannot be removed

Iam using below code

string Salt = "Kosher", HashAlgorithm = "SHA1";

            int PasswordIterations = 2;
            string InitialVector = "OFRna73m*aze01xY";

            int KeySize = 256;
            if (string.IsNullOrEmpty(CipherText))
                return "";
            byte[] InitialVectorBytes = System.Text.Encoding.ASCII.GetBytes(InitialVector);
            byte[] SaltValueBytes = System.Text.Encoding.ASCII.GetBytes(Salt);

            byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
            byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
            int ByteCount = 0;
            using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
            {
                using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
                {
                    using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
                    {

                        ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
                        MemStream.Close();
                        CryptoStream.Close();
                    }
                }
            }
            SymmetricKey.Clear();
            return System.Text.Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);





error happening in

 ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) this line as  <br />
<br />
<br />
Padding is invalid and cannot be removed<br />
<br />
<br />
Any body know why error like this arising?<br />
<br />
Thanks in Advance

推荐答案

中发生错误,这可能会帮助您
This might help you, http://forums.asp.net/t/1019434.aspx/1[^]


您正在尝试从流中读取比实际编码更多的字节,因为您将纯文本长度设置为与密码长度相同,并且密码包括填充到下一个块大小.

CrpytoStream是否允许您读取Length属性?如果是这样,使用它来设置PlainTextString的大小.如果不是这样,则在写出加密版本时必须将其保存在某个地方,以便可以读回该数字并使用它来调整PlainTextString的大小.

另外,请确保将密钥,IV,算法等设置为相同,因为如果不这样做,您将获得不透明的解密失败"异常的集合之一.
You are trying to read more bytes out of the stream than are actually encoded, because you set the plain text length to the same as the cipher length, and the cipher includes padding to the next block size.

Does CrpytoStream let you read the Length property? If so, use that to set the size of PlainTextString. If not, you''ll have to save that somewhere when you write out the encrypted version, so you can read the number back and use that to size PlainTextString.

Also, make sure you set up the keys, IV, algorithm etc up the same, because if you don''t you will get one of a collection of opaque ''decryption failed'' exceptions.


这篇关于显示错误填充无效,无法删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆