Has anybody implemented the asymmetic encryption here?
I just tried and I kept getting error whenever I reached
rsa.ToXmlString();
It kept on saying the file is not found. The same thing happens to
rsa.FromXmlString();
it works on localhost, but on jodo, for FromXmlString(), I got this permission thing, I fear it's that dreadful medium trust again, though I can't be sure. So if you had it working, please do let me know, mucho gracias.
below is my code
private RSACryptoServiceProvider rsa;
public string EncryptData(string data2Encrypt)
{
//AssignParameter();
rsa = new RSACryptoServiceProvider();
StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testpublickey.xml"));
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
//read plaintext, encrypt it to ciphertext
byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
}
public void AssignNewKey()
{
// AssignParameter();
rsa = new RSACryptoServiceProvider();
//provide public and private RSA params
string publicPrivateKeyXML = rsa.ToXmlString(true);
StreamWriter writer = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testprivatekey.xml"));
writer.Write(publicPrivateKeyXML);
writer.Close();
//provide public only RSA params
string publicOnlyKeyXML = rsa.ToXmlString(false);
writer = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testpublickey.xml"));
writer.Write(publicOnlyKeyXML);
writer.Close();
}
public string DecryptData(string data2Decrypt)
{
//AssignParameter();
rsa = new RSACryptoServiceProvider();
byte[] getpassword = Convert.FromBase64String(data2Decrypt);
StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testprivatekey.xml"));
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
//read ciphertext, decrypt it to plaintext
byte[] plain = rsa.Decrypt(getpassword, false);
return System.Text.Encoding.UTF8.GetString(plain);
}
I just tried and I kept getting error whenever I reached
rsa.ToXmlString();
It kept on saying the file is not found. The same thing happens to
rsa.FromXmlString();
it works on localhost, but on jodo, for FromXmlString(), I got this permission thing, I fear it's that dreadful medium trust again, though I can't be sure. So if you had it working, please do let me know, mucho gracias.
below is my code
private RSACryptoServiceProvider rsa;
public string EncryptData(string data2Encrypt)
{
//AssignParameter();
rsa = new RSACryptoServiceProvider();
StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testpublickey.xml"));
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
//read plaintext, encrypt it to ciphertext
byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
}
public void AssignNewKey()
{
// AssignParameter();
rsa = new RSACryptoServiceProvider();
//provide public and private RSA params
string publicPrivateKeyXML = rsa.ToXmlString(true);
StreamWriter writer = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testprivatekey.xml"));
writer.Write(publicPrivateKeyXML);
writer.Close();
//provide public only RSA params
string publicOnlyKeyXML = rsa.ToXmlString(false);
writer = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testpublickey.xml"));
writer.Write(publicOnlyKeyXML);
writer.Close();
}
public string DecryptData(string data2Decrypt)
{
//AssignParameter();
rsa = new RSACryptoServiceProvider();
byte[] getpassword = Convert.FromBase64String(data2Decrypt);
StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/testprivatekey.xml"));
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
//read ciphertext, decrypt it to plaintext
byte[] plain = rsa.Decrypt(getpassword, false);
return System.Text.Encoding.UTF8.GetString(plain);
}