RSA Encryption Public and Private Key

liming

Perch
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);

}
 
Yes we are pretty sure it is medium trust but we have been trying to run down exactly where.

I am not sure if you are the one with the ticket in about this, but there as been a ticket for about 4-5 days with this and I have set a number of permissions and not gotten to far with it. But I have not given up either.
 
Thanks Stephen, that wasn't me :) Good to know that there are more persistent ppl out there though, lol.

From other forum I've seen, looks like there is no solution to this issue. From what I've read, you gotta grant permission to a paticular my document of direcory like

\Documents and Settings\{UserName}\Application Data\Microsoft\Crypto\RSA\MachineKeys

that's probably not ideal for hosting. I'm deveoping a software, and I need to distribute it to other hosting clients, so I can't just make it work on jodo since my clients' hosting companies might block it as well.

Thanks a lot man. For now, I'll have to go with symmetic encryption.
 
Actually, I have granted that permissions set already as advised in my research, and it was not the solution. In fact I do this from the start of each server, I just did a reset to ensure it was made.
 
Back
Top