Search code examples
.netinteropcryptographycrypto++

Can I use RSACryptoServiceProvider public/private keys to interop with Crypto++?


I've created a public/private key using RSACryptoServiceProvider and saved it as XML. I can use this to encrypt/decrypt/sign and verify data in .NET.

I have another application which is intended to run on a Linux server. I'm developing it in C++ and I'm using Crypto++ for my cryptography needs.

I want to share data between these two applications, but to do that I need to convert the RSAParameters into public/private keys that Crypto++ can understand.

I'm also open to using convert public/private keys generated by Crypto++ into RSAParameters if it is easier to do.

Any ideas?


Solution

  • The easiest way is probably to use the RSAParameters directly instead of going over XML.

    Just store the byte-arrays contained in the RSAParameters in a file using a format of your own choice. Read the byte-arrays back in your C++ program and create CryptoPP::Integers from them. Use those Integers to Initialize a RSAFunction (public key) or InvertibleRSAFunction (private key).

    The reverse way is similiar: I.e.

    RSAFunction publicKey = ...;
    
    size_t modulusLength = publicKey.getModulus().ByteCount();
    unsigned char * modulus = new unsigned char[modulusLength];
    publicKey.getModulus().Encode(modulus, modulusLength);
    
    // similarly with PublicExponent and remember to clean up the arrays.
    

    and transfer the arrays to your .NET-application, where you can use them to initialize an RSAParameters.