Hi,
I am trying to Encrypt /Decrypt the string using RSA Algoritham i found in some site.
if i Run the same code in Windows Application, it works good. (I can save the Encrypted string in SQL Server, and fetch and Decrypt it back to original string).
But, I am facing the "Bad data" exception in Web Application.
kindly Help...
Below is the source code:
//**** Code for Encrypting Decrypting using RSA Algorithm *****
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace RSA_EncryptionDecryption
{
public class RSA_EncryptDecrypt
{
static public byte[] RSAEncryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
try
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKey);
encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
}
return encryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] RSADecryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
try
{
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKey);
decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
}
return decryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
}
//This is code to pass String as parameter to Encrypt/Decrypt function..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using RSA_EncryptionDecryption;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] plaintext;
byte[] encryptedtext;
byte[] encryptedtext_fromDB;
string Connstr = "Data Source=MyPC;Initial Catalog=MyDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
lblError.Text = "";
TxtForEncryption.Text = "";
LblDecryptedText.Text = "";
}
}
protected void TxtForEncryption_TextChanged(object sender, EventArgs e) { }
#region Encrypt
protected void CmdEncrypt_Click(object sender, EventArgs e)
{
plaintext = ByteConverter.GetBytes(TxtForEncryption.Text);
encryptedtext = RSA_EncryptDecrypt.RSAEncryption(plaintext, RSA.ExportParameters(false), false);
LblEncrptedText.Text = ByteConverter.GetString(encryptedtext);
using (SqlConnection cn = new SqlConnection(Connstr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SAVEPASSWORD";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UPassword", encryptedtext);
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
}
}
}
#endregion
#region Decrypt
protected void CmdDecrypt_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(Connstr))
{
string Q = "select UPassword from USER_PASS where Userid in(select Max(userid) from USER_PASS)";
using (SqlCommand cmd = new SqlCommand(Q, cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
cn.Open();
da.Fill(dt);
cn.Close();
int x = dt.Rows.Count;
foreach (DataRow dr in dt.Rows)
{
encryptedtext_fromDB = (byte[])dr["UPassword"];
}
}
}
}
}
byte[] decryptedtex = RSA_EncryptDecrypt.RSADecryption(encryptedtext_fromDB, RSA.ExportParameters(true), false);
LblDecryptedText.Text = ByteConverter.GetString(decryptedtex);
}
#endregion
}