we migrated our web-application from a .net1 to a .net4 with ISS7 and windows 7 in VS2010.
Now the Rijndaelmanaged decryption is not working correct everytime. We use it to encrypt and decrypt passwords. It works passwords created with the old version. When I create a new user with the .net4 version everythings goes correct but when I use the
password to login, it gives me the errror 'padding is invalid and cannot be removed'. The system first goes to Encrypt the password, get the data from the database, this all goes ok, but then he Decrypt the password and I get the error.
My encryption code is :
using System;
using System.IO;
using System.Security.Cryptography;
{
/// <summary>
///
/// </summary>
public class Encryption
{
private ICryptoTransform EncryptorTransform, DecryptorTransform;
private System.Text.UTF8Encoding UTFEncoder;
static Encryption()
{
}
/// <summary>
/// Method to encrypt a string
/// </summary>
/// <param name="StringToEncrypt">The string to encrypt</param>
/// <returns>The encrypted string</returns>
public static String Encrypt(String StringToEncrypt)
{
RijndaelManaged rmCrypto = new RijndaelManaged();
return Crypt(rmCrypto.CreateEncryptor(Constants.Key, Constants.IV), StringToEncrypt);
}
/// <summary>
/// Method to decrypt a string
/// </summary>
/// <param name="StringToDecrypt">The string to decrypt</param>
/// <returns>The decrypted string</returns>
public static String Decrypt(String StringToDecrypt)
{
RijndaelManaged rmCrypto = new RijndaelManaged();
return Crypt(rmCrypto.CreateDecryptor(Constants.Key, Constants.IV), StringToDecrypt);
}
private static String Crypt(ICryptoTransform cryptMethod, String StringToCrypt )
{
MemoryStream memStream = new MemoryStream();
//System.Diagnostics.Debug.WriteLine(StringToCrypt);
CryptoStream crStream = new CryptoStream(
memStream,
cryptMethod,
CryptoStreamMode.Write
);
byte[] byteArrayToCrypt = System.Text.UnicodeEncoding.Unicode.GetBytes(StringToCrypt);
crStream.Write(byteArrayToCrypt, 0, byteArrayToCrypt.Length);
//crStream.FlushFinalBlock();
//crStream.Dispose();
crStream.Close();
byte[] encResult = memStream.ToArray();
//System.Diagnostics.Debug.WriteLine(System.Text.UnicodeEncoding.Unicode.GetString(encResult, 0, encResult.Length));
return System.Text.UnicodeEncoding.Unicode.GetString(encResult, 0, encResult.Length);
}
}
}
I looked on the internet for several hours but no solution is working for me. Can someone please help me with this ?
SuzyThys
0 Points
1 Post
Rijndaelmanaged migrate from .Net1 to .Net 4 gives error padding is invalid and cannot be removed
Feb 22, 2012 08:53 AM|LINK
we migrated our web-application from a .net1 to a .net4 with ISS7 and windows 7 in VS2010.
Now the Rijndaelmanaged decryption is not working correct everytime. We use it to encrypt and decrypt passwords. It works passwords created with the old version. When I create a new user with the .net4 version everythings goes correct but when I use the password to login, it gives me the errror 'padding is invalid and cannot be removed'. The system first goes to Encrypt the password, get the data from the database, this all goes ok, but then he Decrypt the password and I get the error.
My encryption code is :
using System; using System.IO; using System.Security.Cryptography; { /// <summary> /// /// </summary> public class Encryption { private ICryptoTransform EncryptorTransform, DecryptorTransform; private System.Text.UTF8Encoding UTFEncoder; static Encryption() { } /// <summary> /// Method to encrypt a string /// </summary> /// <param name="StringToEncrypt">The string to encrypt</param> /// <returns>The encrypted string</returns> public static String Encrypt(String StringToEncrypt) { RijndaelManaged rmCrypto = new RijndaelManaged(); return Crypt(rmCrypto.CreateEncryptor(Constants.Key, Constants.IV), StringToEncrypt); } /// <summary> /// Method to decrypt a string /// </summary> /// <param name="StringToDecrypt">The string to decrypt</param> /// <returns>The decrypted string</returns> public static String Decrypt(String StringToDecrypt) { RijndaelManaged rmCrypto = new RijndaelManaged(); return Crypt(rmCrypto.CreateDecryptor(Constants.Key, Constants.IV), StringToDecrypt); } private static String Crypt(ICryptoTransform cryptMethod, String StringToCrypt ) { MemoryStream memStream = new MemoryStream(); //System.Diagnostics.Debug.WriteLine(StringToCrypt); CryptoStream crStream = new CryptoStream( memStream, cryptMethod, CryptoStreamMode.Write ); byte[] byteArrayToCrypt = System.Text.UnicodeEncoding.Unicode.GetBytes(StringToCrypt); crStream.Write(byteArrayToCrypt, 0, byteArrayToCrypt.Length); //crStream.FlushFinalBlock(); //crStream.Dispose(); crStream.Close(); byte[] encResult = memStream.ToArray(); //System.Diagnostics.Debug.WriteLine(System.Text.UnicodeEncoding.Unicode.GetString(encResult, 0, encResult.Length)); return System.Text.UnicodeEncoding.Unicode.GetString(encResult, 0, encResult.Length); } } }I looked on the internet for several hours but no solution is working for me. Can someone please help me with this ?
Thank you in advance!
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: Rijndaelmanaged migrate from .Net1 to .Net 4 gives error padding is invalid and cannot be rem...
Feb 24, 2012 04:42 AM|LINK
Hi,
The following example demonstrates how to encrypt and decrypt sample data using the RijndaelManaged class.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx
http://www.codeproject.com/Articles/3740/Cryptography-in-NET-part-1
You can also look at related errors :
http://forums.asp.net/t/1019434.aspx
http://forums.asp.net/t/1378707.aspx/1
http://forums.asp.net/t/1064000.aspx/1/10
I hope the above information helps
Feedback to us
Develop and promote your apps in Windows Store