Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi...
i am using rijndeal to encrypt and decrypt some data! but it gives me this error :
HTML
Padding is invalid and cannot be removed.

i searched much but nothing help me to solve this error! tis is my encrypt/decrypt codes:

C#
public string Encrypt(string text)
    {

        mainRM = new System.Security.Cryptography.RijndaelManaged();
        mainRM.BlockSize = 256;
        mainRM.KeySize = 256;
        memorystream = new System.IO.MemoryStream();
        ICryptoTransform icrypt = mainRM.CreateEncryptor(key, iv);
        CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Write);
        cryptstream.FlushFinalBlock();
        System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptstream);
        sw.Write(text);
        return Convert.ToBase64String(memorystream.ToArray());
    }

    public string Decrypt(string CryptedText)
    {
        string custinfo;
        System.IO.StreamReader streamreader;
        mainRM = new RijndaelManaged();
        mainRM.BlockSize = 256;
        mainRM.KeySize = 256;
        memorystream = new System.IO.MemoryStream(Convert.FromBase64String(CryptedText));
        ICryptoTransform icrypt = mainRM.CreateDecryptor(key, iv);
        memorystream.Position = 0;
        CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Read);
        cryptstream.FlushFinalBlock();
        streamreader = new System.IO.StreamReader(cryptstream);
        custinfo = streamreader.ReadToEnd();
        return custinfo;
    }


can anyone help me?
Posted
Updated 31-Dec-19 1:00am
Comments
StianSandberg 26-Apr-13 9:20am    
Why do you modify the Blocksize and Keysize?
ali_heidari_ 26-Apr-13 9:48am    
you mean, i leave Blocksize and Keysize in default value?
StianSandberg 26-Apr-13 9:52am    
yes. Do you know why you modify this values?
ali_heidari_ 26-Apr-13 9:59am    
this algorthm will make the data to some smaller blocks! blocksize will set lenght of this small blocks. Keysize sets size of encrypted data key value! right?
StianSandberg 26-Apr-13 10:00am    
yes, but why would you change those values?

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:
C#
mainRM.Padding = PaddingMode.None;


Here's some code I used in a project a few months ago. It'll do the job:

C#
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);
}
 
Share this answer
 
v2
Comments
ali_heidari_ 26-Apr-13 9:46am    
i did... but its give me encrypted data null, freespaces! encrypted text doesnt have any charecter! and when i decrypt ,resault is empty string!
ali_heidari_ 26-Apr-13 10:34am    
thank you very much! its working ! but it was verry better if you keep some comments on your codes! and an important notice about your code is, the key must be 16 charecter!
Line 52: byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900