加密填充无效,无法删除

时间:2014-03-24 06:13:56

标签: c# encryption windows-phone-8 aes

我正在尝试解密Windows Phone 8中的字符串。但不幸的是它给了我以下错误

  

System.Security.Cryptography.CryptographicException:填充无效且无法删除。      在System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte [] inputBuffer,Int32 inputOffset,Int32 inputCount,Byte []& outputBuffer,Int32 outputOffset,PaddingMode paddingMode,Boolean fLast)      在System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte [] inputBuffer,Int32 inputOffset,Int32 inputCount)      在System.Security.Cryptography.CryptoStream.FlushFinalBlock()      在SampleAESEncryption.AES256.Decrypt(String dataToDecrypt,String password,String salt)      在SampleAESEncryption.MainPage.btnDecrypt_Click(对象发送者,RoutedEventArgs e)      在System.Windows.Controls.Primitives.ButtonBase.OnClick()      在System.Windows.Controls.Button.OnClick()      在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)      在System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl,EventArgs e)      在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,String eventName)

这是我的代码。 txtText.Text 采用256位加密。

 txtText.Text = "Y5tq+5Smr13ChO2KYTOxvbCBlRTIDFXf+Ott2Euq+HiXTHDtUXn2+E46CYCGSC7P";
 private void btnDecrypt_Click(object sender, RoutedEventArgs e)
 {
        AES256 encryptor = new AES256();

        string strBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(txtText.Text.Trim()));
        string decryptedString = encryptor.Decrypt(strBase64, "12345678", "12345678");

        txtText.Text = decryptedString;
 }

解密方法

 public string Decrypt(string dataToDecrypt, string password, string salt)
    {
        AesManaged aes = null;
        MemoryStream memoryStream = null;

        try
        {
            //Generate a Key based on a Password and HMACSHA1 pseudo-random number generator
            //Salt must be at least 8 bytes long
            //Use an iteration count of at least 1000
            Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

            //Create AES algorithm
            aes = new AesManaged();
            //Key derived from byte array with 32 pseudo-random key bytes
            aes.Key = rfc2898.GetBytes(32);
            //IV derived from byte array with 16 pseudo-random key bytes
            aes.IV = rfc2898.GetBytes(16);

            //Create Memory and Crypto Streams
            memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

            //Decrypt Data
            byte[] data = Convert.FromBase64String(dataToDecrypt);
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();

            //Return Decrypted String
            byte[] decryptBytes = memoryStream.ToArray();

            //Dispose
            if (cryptoStream != null)
                cryptoStream.Dispose();

            //Retval
            return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
        }
        finally
        {
            if (memoryStream != null)
                memoryStream.Dispose();

            if (aes != null)
                aes.Clear();
        }
    }

我尝试了很多,但无法解决这个问题。我怎么解决这个问题?我的解密方法有什么问题吗?

1 个答案:

答案 0 :(得分:1)

Convert.ToBase64String(Encoding.UTF8.GetBytes(txtText.Text.Trim()));

这几乎肯定是一个错误。您应该使用Convert.FromBase64从数据中获取字节数组,对其进行解密,然后使用Encoding.UTF8.GetString将结果转换为字符串。加密时,请执行相反的操作:使用Encoding.UTF8.GetBytes获取字节,对其进行加密,然后将结果转换为Convert.ToBase64String的字符串。