System.IO.IOException: -----END RSA PRIVATE KEY not found

If this doesn't work "-----BEGIN RSA PRIVATE KEY-----\r\nXXXXXXXX\r\n-----END RSA PRIVATE KEY-----"

please try this "-----BEGIN RSA PRIVATE KEY-----
XXXXXXXX
-----END RSA PRIVATE KEY-----"


You need to add a new line between the pre/post encapsulation boundary text and the Base64 data, so:

 string privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nXXX\r\n-----END RSA PRIVATE KEY-----";

This is because the pem specification allows for the existence of other textual headers between the two.