1

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!!

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
Aposchi8
  • 41
  • 1
  • 3
  • 1
    What's the exact exception you are getting? – 500 - Internal Server Error Jun 29 '18 at 10:37
  • The principle is identical to https://stackoverflow.com/questions/46948083/how-to-decrypt-using-rsa-from-pem-file to import the key. It's a pem file, not an XML file – Anya Shenanigans Jun 29 '18 at 10:37
  • 2
    Possible duplicate of [How to decrypt using rsa from PEM file](https://stackoverflow.com/questions/46948083/how-to-decrypt-using-rsa-from-pem-file) – Anya Shenanigans Jun 29 '18 at 10:37
  • there is a very complete example also [here](https://stackoverflow.com/questions/17128038/c-sharp-rsa-encryption-decryption-with-transmission)... – Antonino Jun 29 '18 at 10:41
  • The Exception is:System.Security.XmlSyntaxException: "Ungültige Syntax in der Zeile 1." – Aposchi8 Jun 29 '18 at 10:59
  • @Petesh : The keys mentioned here are in a format like -----BEGIN PUBLIC KEY----- Therefore I am not sure how to work with it, since mine start with BEGIN CERTIFICATE – Aposchi8 Jun 29 '18 at 11:01
  • You can extract just the public key using openssl commands e.g. `openssl x509 -in cert.pem -pubkey -text -noout` (the first few lines contain the public key). – Anya Shenanigans Jun 29 '18 at 11:08
  • So I would save my string with the key to a *.pem file. And then I can use openssl with it? – Aposchi8 Jun 29 '18 at 11:18
  • Your blob is lying about what it is. It is not a certificate, it is a X509 SubjectPublicKeyInfo structure. It should say `-----BEGIN PUBLIC KEY-----`. Therefore @Petesh was correct about this being a duplicate. Just follow the answer there. – President James K. Polk Jun 29 '18 at 22:19

1 Answers1

0
public static string ConvertToXmlPublicJavaKey(string publicJavaKey)
{
    RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicJavaKey));
    string xmlpublicKey = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
        Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
        Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
    return xmlpublicKey;
}

The PublicKeyFactory class is from BouncyCastle.Crypto, you can Google it for more information.

Then your rsaPublic.FromXmlString(publicKey) will work fine.

guogangj
  • 2,275
  • 3
  • 27
  • 44