RSA (ASP.NET)
RSA is an asymmetric encyption algorithm, which uses two keys, one to encrypt and the other
to decrypt. It was created in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman, and is still one of the most widely used encryption methods. A typical application is in authenticating a sender, where the senders private key is used to encrypt a message, and then is decrypted by the receiver with the senders public key (which is known to anyone who wants it). It is also typically used for encrypting disks/files, such as for EFS.
Note: The RSA crypto function is restricted on an ASP.NET server. For a Windows version: [Click here]
Message |
|
| |
|
| |
|
Public key |
|
|
Private key |
|
|
|
|
Encrypted |
Decrypted |
|
|
|
Ref of keys [Link].
This works fine on my machine, but when it is ported to the Web server it gives:
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission,
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
and this is because the Web hostering providers block the loading of private keys
as it could compromise the security of the Windows key store, thus most Web hosting
providers turn it off so that it is not possible to comprise the system private
keys.
The code is:
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.Collections;
using System.Security.Cryptography;
using System.IO;
using System.Text;
public partial class _Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters publickey = rsa.ExportParameters(false); // don't export private key
RSAParameters privatekey = rsa.ExportParameters(true); // export private key
this.tbPublicKey.Text = "e=" + ByteToString(publickey.Exponent) + ", n=" + ByteToString(publickey.Modulus);
this.tbPrivateKey.Text = "d=" + ByteToString(privatekey.D) + ", n=" + ByteToString(publickey.Modulus);
rsa.ImportParameters(publickey);
byte[] encryptedData = rsa.Encrypt(StringToByte(this.tbMessage.Text), true);
this.tbEncrypt.Text = ByteToString(encryptedData);
rsa.ImportParameters(privatekey);
byte[] decryptedData = rsa.Decrypt(encryptedData, true);
this.tbDecrypt.Text = ByteToString2(decryptedData);
}
catch (Exception ex)
{
this.tbEncrypt.Text = ex.Message.ToString();
}
}
public static byte[] StringToByte(string StringToConvert)
{
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[CharArray.Length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static byte[] StringToByte(string StringToConvert, int length)
{
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("x2"); // hex format
}
return (sbinary);
}
public static string ByteToString2(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += (char)buff[i]; // hex format
}
return (sbinary);
}
}
|