I am trying to RSA
Encrypt a string with a given key. (I can not change the key, since I request it from another system)
I get the key as a string and it looks like this:
-----BEGIN CERTIFICATE-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSehIDcXho52VvAQAFfVD2CzOFAYKSfnRsuKE/nqE1O4l/X6opYgjxj/mSNk1bvNobwiRBL4uWfFnsHoQSiv1Gqzl+JQ2QQ2rUVGuNZ7d7agjYcb1LCpKPE1Q0kqLvbGyDWQx8ULC7/FJ49mMwbzIE4C9ovfdOBa0er6IDNSW0IQIDAQAB-----END CERTIFICATE-----
Now I tried to encrypt the string on many ways, but none of them worked. The last solution I tried was the following:
public static string RSA(string payload, string publicKey)
{
byte[] toEncryptData = Encoding.ASCII.GetBytes(payload);
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();
rsaPublic.FromXmlString(publicKey);
byte[] encryptedRSA = rsaPublic.Encrypt(toEncryptData, false);
string EncryptedResult = Encoding.Default.GetString(encryptedRSA);
return EncryptedResult;
}
It always throws an Exception when it comes to:
rsaPublic.FromXmlString(publicKey);
So what can I do to encrypt the string with RSA? I need it because I want to implement an API: https://www.loxone.com/dede/wp-content/uploads/sites/2/2016/08/0903_Communicating-with-the-Miniserver.pdf
On Page 5 under 6. you can see the requirement!!