B
Bishoy George
I made a class based on RijndaelManaged class.
I tied to separate the encrypting and decrypting processes.
I now have the follwing resistant error:
Length of the data to decrypt is invalid
Line 70: cs.Read(fromEncrypted, 0, fromEncrypted.Length);
I need a fix please.....
The Code:
--------------------------------------------------
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 = Convert.ToBase64String(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 = Convert.ToBase64String(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"];
}
}
I tied to separate the encrypting and decrypting processes.
I now have the follwing resistant error:
Length of the data to decrypt is invalid
Line 70: cs.Read(fromEncrypted, 0, fromEncrypted.Length);
I need a fix please.....
The Code:
--------------------------------------------------
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 = Convert.ToBase64String(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 = Convert.ToBase64String(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"];
}
}