I am getting following error
Padding is invalid and cannot be removed
however, I have checked during debugging that all properties were same in encryption and decryption i.e; BlockSizeValue = 128
, InputBlockSize = 16
, OutputBlockSize = 16
, m_cipherMode = CBC
, m_paddingValue = PKCS7
etc.
I have also tried by providing above values in code, with padding to None, Zero and PKCS7. None
and Zeros
give an output with strange chracters
Even the values of encrypted byte[] are same on both calls.
here is code
Call from Windows form application
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";
}
}
Crypto project
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;
}
}
}