Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi...
i am using rijndeal to encrypt and decrypt some data! but it gives me this error :
Padding is invalid and cannot be removed.
i searched much but nothing help me to solve this error! tis is my encrypt/decrypt codes:
 
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 26-Apr-13 4:15am
Comments
AlluvialDeposit at 26-Apr-13 9:20am
   
Why do you modify the Blocksize and Keysize?
source.compiler at 26-Apr-13 9:48am
   
you mean, i leave Blocksize and Keysize in default value?
AlluvialDeposit at 26-Apr-13 9:52am
   
yes. Do you know why you modify this values?
source.compiler at 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?
AlluvialDeposit at 26-Apr-13 10:00am
   
yes, but why would you change those values?
source.compiler at 26-Apr-13 10:06am
   
maybe i should not but when i try to leave them as default, my encrypted data is some freespaces, empty! when i decrypt resault is empty string! well whats your solution?
charles henington at 26-Apr-13 10:21am
   
the iv of rijndael must be set to 16 where the key can be set to 16 or 32, have never had a reason to change the blocksize, I will provide a few encrypt and decrypt methods that will accept a password to protect the data
source.compiler at 26-Apr-13 10:19am
   
when i dnt set these, i recieve this error : Specified initialization vector (IV) does not match the block size for this algorithm.
charles henington at 26-Apr-13 12:53pm
   
you should set the iv to 16 and the key to either 16 0r 32
 
public string Encrypt128(string Data, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(Data);
 

PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
 

new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
MemoryStream ms = new MemoryStream();
 
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(16);
 
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
 
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return Convert.ToBase64String(encryptedData);
 

}
or you could also use
 
public string Encrypt256(string Data, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(Data);
 

PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
 

new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
MemoryStream ms = new MemoryStream();
 
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
 
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
 
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return Convert.ToBase64String(encryptedData);
 

}

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
}
  Permalink  
v2
Comments
source.compiler at 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!
source.compiler at 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!

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100