1
votes

I am trying to decrypt string in windows phone 8. But unforunatly it gives me following error

System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at SampleAESEncryption.AES256.Decrypt(String dataToDecrypt, String password, String salt) at SampleAESEncryption.MainPage.btnDecrypt_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Here is my code. txtText.Text is in 256 bit encrypted.

 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;
 }

Decrypt Method

 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();
        }
    }

I tried a lot but not able to solve this issue. How can I solve this? Is any thing wrong in my decrypt method?

1

1 Answers

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

This is almost certainly a mistake. You're supposed to use Convert.FromBase64 to get a byte array from your data, decrypt it, then use Encoding.UTF8.GetString to convert the result to string. When encrypting, do it the other way: get the bytes with Encoding.UTF8.GetBytes, encrypt them, then convert the result to string with Convert.ToBase64String.