Rijndael/AES is a block cypher. It encrypts data in 128 bit (16 character) blocks. Cryptographic padding is used to make sure that last block of the message is always the correct size.
You need to explicitly set the padding for both encryption and decryption.
For example:
mainRM.Padding = PaddingMode.None;
Here's some code I used in a project a few months ago. It'll do the job:
public static string Encrypt(string toEncrypt, string securityKey)
{
var key = securityKey;
var keyArray = Encoding.UTF8.GetBytes(key);
var tdes = new TripleDESCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = tdes.CreateEncryptor();
var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string cipherString, string securityKey)
{
var key = securityKey;
var keyArray = Encoding.UTF8.GetBytes(key);
var tdes = new TripleDESCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = tdes.CreateDecryptor();
var toEncryptArray = Convert.FromBase64String(cipherString);
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Encoding.UTF8.GetString(resultArray);
}