0

I want to sign some data with RSA.SignHash(). I've created a RSA private key file via the following command in Linux OpenSSL.

openssl genrsa -out private_key.pem 1024

But when I want to load this key by an object of X509Certificate2, get the "Cannot find the requested object." exception. Here is my code:

string keyFilePath = @"C:\Keys\private_key.pem";
X509Certificate2 cert = new X509Certificate2(keyFilePath);

I've used the Chilkat library and everything works fine, But doesn't Microsoft have any walk-though to approach it without any third-party tools? Here is my pem file content:

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC23ivEPgSqxCcSTNUY8IbjCO0+0FwarI/azCHp97cWCBaosFAe
mN9eI7u3agv2tCk+QrKrFDlkYWskFiADm55NMvDE1fuyy7db84MMh+GFcyemYZ0o
yG5oBYPw7aNY7N7fBO5pbF9M/v4TMjKoxYGxk2kl58KT9cmvVH7TaF8xkwIDAQAB
AoGAfVzRpDbf/DF8l48Uh4Rc9EeqXOV6Ps8Nz3EhzsODQBdLcVltk4w5lM/qYFLS
4M2heI1A7wduUOJ4EMUaLV8BpcFkA2kSPah48R/1EGAMOpaD09j8qHJLCHJ2uiSc
Hqi6z/6GhpSLzU5XR1lj0UIPy9aC9X0yPAao0WZ+5YgTdAECQQDoCwCmFD5r00Y0
DcQGbAU/eekHJwNk48ACvIDquK/ysqA8aXvWgyXd3oa82fUSvSAMDGNFYr9xOxqF
Vnnsi6VXAkEAyb9yymbehjwwl33CjPcsIRbjetLDwPgWhVYii4q7jELlefyC8mHL
cpn0ejS4ln/uTUuNZCdYNHvYjQ8eXfaUJQJADfM7YsCs0AavncmGE2zDFAHcRJXP
2mzmykNS7MmVql2azIb67vaLfD84knn4Bdxg5NiJz04UfFY1TfbY9aOfmQJBAJGX
jsCQMiBPSYXZ5M+UBI2wleNqPIiCwMXinjVzndsf37kDyIAgoRCIGA0lBNzfX9r6
HgRb/GSLx4Asm+6VZt0CQQC2DwtG+CKDyqEvhIGqlrAuQivMLJExZZV3kjsFSWOA
CZFSr8JsSghb3bbxYexkzbWfalESYRkqu+zNWDPs9gpt
-----END RSA PRIVATE KEY-----
Soheil Farahani
  • 349
  • 1
  • 2
  • 13
  • Its the pem format that is the issue, see: [how to get private key from PEM file?](https://stackoverflow.com/questions/7400500/how-to-get-private-key-from-pem-file) – Alex K. May 30 '17 at 13:05
  • Dear @AlexK. and Simone Cifani, I've tried the other article (https://stackoverflow.com/questions/7400500/how-to-get-private-key-from-pem-file). And same exception. I do not have any Certificate. I have a private key. – Soheil Farahani May 30 '17 at 13:10
  • Just stating the obvious: a key is not a certificate. It does not make sense to try to load a private key into a certificate object. – jww May 30 '17 at 13:44
  • Dear @jww please direct me how to sign with just a private key. – Soheil Farahani May 30 '17 at 13:46
  • [Signing and verifying signatures with RSA C#](https://stackoverflow.com/q/8437288/608639), [how to sign bytes using my own rsa private key using rs256 algorithm?](https://stackoverflow.com/q/25909044/608639), [Signing data with private key in c#](https://stackoverflow.com/q/31828420/608639), [How can I sign a file using RSA and SHA256 with .NET?](https://stackoverflow.com/q/7444586/608639), [Signing a string with RSA private key on .NET?](https://stackoverflow.com/q/3169829/608639), etc. – jww May 30 '17 at 13:53

1 Answers1

1

No, there is no friendly way to do it without third-party tools. If you only have private key then IMHO new X509Certificate2(keyFilePath) does not make sense.

What you could do is implement (or use a library) to parse the private key. Then encode parsed private key parameters to a xml structure that can be imported using RSA.FromXmlString.

pepo
  • 8,644
  • 2
  • 27
  • 42