Tabii mümkün...
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//ASCIIEncoding ByteConverter = new ASCIIEncoding(); //128 byte hatası devam ediyor. Array can not be null.
//UnicodeEncoding ByteConverter = new UnicodeEncoding(); //128 byte hatası devam ediyor. Array can not be null.
UTF8Encoding ByteConverter = new UTF8Encoding(); //128 byte hatası devam ediyor. Array can not be null.
byte[] dataToEncrypt;
byte[] dataToDecrypt;
byte[] encryptedData;
byte[] decryptedData;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dataToEncrypt = ByteConverter.GetBytes("ofof");
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(true), false);
Label1.Text = ByteConverter.GetString(encryptedData);
dataToDecrypt = encryptedData;
decryptedData = RSADecrypt(dataToDecrypt, RSA.ExportParameters(true), false);
if(decryptedData != null)
Label2.Text = ByteConverter.GetString(decryptedData);
Label2.Text += "<br/>";
dataToDecrypt = ByteConverter.GetBytes(Label1.Text.Trim());
decryptedData = RSADecrypt(dataToDecrypt, RSA.ExportParameters(true), false);
//decryptedData hata oluşnca null geliyor.
if(decryptedData != null)
Label2.Text += ByteConverter.GetString(decryptedData);
HttpCookie hc = new HttpCookie("Temp");
hc.Values.Add("TempPassword", ByteConverter.GetString(encryptedData));
hc.Expires = DateTime.Now.AddDays(30); //cookie 30 gün
Response.Cookies.Add(hc);
}
}
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
catch (CryptographicException ex)
{
HttpContext.Current.Response.Write(ex.Message + "<br/>");
return null;
}
}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
catch (CryptographicException ex)
{
HttpContext.Current.Response.Write(ex.Message + "<br/>");
return null;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
HttpCookie hc = Request.Cookies["Temp"];
dataToDecrypt = ByteConverter.GetBytes(hc.Values["TempPassword"]);
//string a = ByteConverter.GetString(dataToDecrypt);
Response.Write(dataToDecrypt.Length);
dataToDecrypt = ByteConverter.GetBytes(hc.Values["TempPassword"]);
decryptedData = RSADecrypt(dataToDecrypt, RSA.ExportParameters(false), false);
try
{
Label2.Text = ByteConverter.GetString(decryptedData);
}
catch (CryptographicException ex)
{
Response.Write(ex.Message + "<br/>");
}
catch (Exception ex)
{
Response.Write(ex.Message + "<br/>");
}
}
}
bize ruyalarimizin degil uyanikligimizin tabiri gerek (Alinti)
|