RijndaelManaged - 填充无效,无法在C#中删除

时间:2018-03-24 11:03:03

标签: c# cryptography rijndaelmanaged

我收到以下错误

  

填充无效且无法删除

但是,我在调试期间检查过所有属性在加密和解密方面都是相同的,即; BlockSizeValue = 128InputBlockSize = 16OutputBlockSize = 16m_cipherMode = CBCm_paddingValue = PKCS7等。

我还试过在代码中提供上面的值,填充为None,Zero和PKCS7。 NoneZeros输出提供了奇怪的字符

即使加密byte []的值在两次调用中都是相同的。

这是代码

从Windows表单应用程序调用

byte[] encrypted;
        string decrypted = "";
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if(txtInput.Text != "" || txtInput.Text != String.Empty)
            {
                if (rbtAES.Checked)
                {
                    if (txtKey.Text != "" || txtKey.Text != String.Empty)
                    {
                    }
                    else
                    {
                        txtOutput.Text = "Key is missing";
                    }

                }
                else if (rbtDES.Checked)
                {

                }
                else if (rbtRijd.Checked)
                {
                    encrypted = RijndaelManagedCryptography.Encrypt(txtInput.Text);
                    txtOutput.Text = "";
                    txtOutput.Text = Convert.ToBase64String(encrypted);
                    //txtOutput.Text = Encoding.ASCII.GetString(encrypted);
                }
                else
                {
                    txtOutput.Text = "Cryptography type is not selected";

                }
            }
            else
            {
                txtOutput.Text = "Write an input for encryption";
            }


        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "" || txtInput.Text != String.Empty)
            {
                if (rbtAES.Checked)
                {
                    if (txtKey.Text != "" || txtKey.Text != String.Empty)
                    {
                    }
                    else
                    {
                        txtOutput.Text = "Key is missing";
                    }

                }
                else if (rbtDES.Checked)
                {

                }
                else if (rbtRijd.Checked)
                {
                    decrypted = RijndaelManagedCryptography.Decrypt(txtInput.Text);
                    txtOutput.Text = "";
                    txtOutput.Text = decrypted;
                }
                else
                {
                    txtOutput.Text = "Cryptography type is not selected";

                }
            }
            else
            {
                txtOutput.Text = "Write an input for decryption";
            }
        }

加密项目

namespace CryptograpgyUnit
{
    public class RijndaelManagedCryptography
    {

        public static byte[] Encrypt(string input)
        {
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {

                myRijndael.GenerateKey();
                myRijndael.GenerateIV();

                // Encrypt the string to an array of bytes. 
                byte[] encrypted = EncryptStringToBytes(input, myRijndael.Key, myRijndael.IV);
                return encrypted;
            }
        }

        public static string Decrypt(string input)
        {

            //byte[] encrypted = Encoding.ASCII.GetBytes(input);
            byte[] encrypted = Convert.FromBase64String(input);
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {

                myRijndael.GenerateKey();
                myRijndael.GenerateIV();

                // Decrypt the bytes to a string. 
                string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                return decrypted;
            }
        }

        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an RijndaelManaged object 
            // with the specified key and IV. 
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption. 
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream. 
            return encrypted;

        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an RijndaelManaged object 
            // with the specified key and IV. 
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }

    }
}

0 个答案:

没有答案