0

I have an existing private key that is being loaded in a C application via an OpenSSL module.

int PRIVATE_KEY_LEN = 512;
RSA *rsa;
unsigned char PRIVATE_KEY[] = { 0x00, 0x01, 0x02, ... }
const unsigned char *Key = PRIVATE_KEY;
rsa = d2i_RSAPrivateKey(NULL, &Key, PRIVATE_KEY_LEN);

From what I understand the private key is in a DER format and I'm now trying to use the key in a separate .NET application.

Is there a way to load this into a RSACryptoServiceProvider without using a third part library in C#?

I have a feeling I'm supposed to create a RSAParameters and load the relevant bytes from the key like so but not really sure which bytes I'm supposed to be using

int PRIVATE_KEY_LEN = 512;
private byte[] PRIVATE_KEY = { 0x00, 0x01, 0x02, ... }
rsa = new RSACryptoServiceProvider();
var rsaParam = rsa.ExportParameters(false);
rsaParam.Modulus = PRIVATE_KEY.Take(PRIVATE_KEY_LEN).ToArray();
rsa.ImportParameters(rsaParam);

Am I way off?

jww
  • 97,681
  • 90
  • 411
  • 885
Puddler
  • 2,619
  • 2
  • 17
  • 26
  • 1
    You have to convert the format Microsoft uses or use third party library such as Bouncy Castle. – Lex Li Jul 04 '16 at 06:54
  • 1
    C# needs the ket with the subjectPublicKeyInfo. You need to use OpenSSL's `i2d_RSAPublicKey_*` or `i2d_RSA_PUBKEY_*` to write the subjectPublicKeyInfo, and not just the public key. – jww Jul 04 '16 at 11:15
  • 1
    Check this blog post ".NET RSACryptoServiceProvider PEM + DER Support": http://www.cnblogs.com/adylee/p/3611461.html – Jim Flood Jul 07 '16 at 01:19

0 Answers0