B
Bishoy George
// I have the following class:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MyEncryption : System.Web.UI.Page
{
public MyEncryption()
{
}
public static string Encrypt(string original)
{
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV;
ASCIIEncoding textConverter = new ASCIIEncoding();
toEncrypt = textConverter.GetBytes(original);
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.GenerateKey();
myRijndael.GenerateIV();
key = myRijndael.Key;
IV = myRijndael.IV;
MyEncryption me = new MyEncryption();
me.SetVariables(key, IV);
MemoryStream ms = new MemoryStream();
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
CryptoStream cs = new CryptoStream(ms, encryptor,
CryptoStreamMode.Write);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
encrypted = ms.ToArray();
string final = textConverter.GetString(encrypted);
return final;
}
public static string Decrypt(string encryptedString)
{
byte[] key;
byte[] IV;
byte[] encrypted;
byte[] fromEncrypted;
MyEncryption me = new MyEncryption();
me.GetVariables(out key, out IV);
ASCIIEncoding textConverter = new ASCIIEncoding();
encrypted = textConverter.GetBytes(encryptedString);
fromEncrypted = new byte[encrypted.Length];
MemoryStream ms = new MemoryStream(encrypted);
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);
CryptoStream cs = new CryptoStream(ms, decryptor,
CryptoStreamMode.Read);
cs.Read(fromEncrypted, 0, fromEncrypted.Length);
string decryptedString = textConverter.GetString(fromEncrypted);
return decryptedString;
}
private void SetVariables(byte[] key, byte[] IV)
{
Session["key"] = key;
Session["IV"] = IV;
}
private void GetVariables(out byte[] key, out byte[] IV)
{
key = (byte[])Session["key"];
IV = (byte[])Session["IV"];
}
}
// the problem is: when I execute the Decrypt Method, I receive the
following error:
// Padding is invalid and cannot be removed.
------------------------------------------------------------------
Bishoy George
(e-mail address removed)
(e-mail address removed)
0102550399
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MyEncryption : System.Web.UI.Page
{
public MyEncryption()
{
}
public static string Encrypt(string original)
{
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV;
ASCIIEncoding textConverter = new ASCIIEncoding();
toEncrypt = textConverter.GetBytes(original);
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.GenerateKey();
myRijndael.GenerateIV();
key = myRijndael.Key;
IV = myRijndael.IV;
MyEncryption me = new MyEncryption();
me.SetVariables(key, IV);
MemoryStream ms = new MemoryStream();
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
CryptoStream cs = new CryptoStream(ms, encryptor,
CryptoStreamMode.Write);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
encrypted = ms.ToArray();
string final = textConverter.GetString(encrypted);
return final;
}
public static string Decrypt(string encryptedString)
{
byte[] key;
byte[] IV;
byte[] encrypted;
byte[] fromEncrypted;
MyEncryption me = new MyEncryption();
me.GetVariables(out key, out IV);
ASCIIEncoding textConverter = new ASCIIEncoding();
encrypted = textConverter.GetBytes(encryptedString);
fromEncrypted = new byte[encrypted.Length];
MemoryStream ms = new MemoryStream(encrypted);
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);
CryptoStream cs = new CryptoStream(ms, decryptor,
CryptoStreamMode.Read);
cs.Read(fromEncrypted, 0, fromEncrypted.Length);
string decryptedString = textConverter.GetString(fromEncrypted);
return decryptedString;
}
private void SetVariables(byte[] key, byte[] IV)
{
Session["key"] = key;
Session["IV"] = IV;
}
private void GetVariables(out byte[] key, out byte[] IV)
{
key = (byte[])Session["key"];
IV = (byte[])Session["IV"];
}
}
// the problem is: when I execute the Decrypt Method, I receive the
following error:
// Padding is invalid and cannot be removed.
------------------------------------------------------------------
Bishoy George
(e-mail address removed)
(e-mail address removed)
0102550399