hetelek
hetelek

Reputation: 3886

RSA C++ equivalent in C#

I have been trying to just get the equivalent of this C# code in C++ for a couple days now, and it's not working out.

Here is my C# code:

RSAParameters rsaParams = new RSAParameters();
rsaParams.Modulus = someByteArray;
rsaParams.Exponent = someByteArray1;
rsaParams.D = someByteArray2;
rsaParams.DP = someByteArray3;
rsaParams.DQ = someByteArray4;
rsaParams.P = someByteArray5;
rsaParams.Q = someByteArray6;
rsaParams.InverseQ = someByteArray7;

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
    rsa.ImportParameters(rsaParams);
    byte[] encryptedData = rsa.Encrypt(toEncrypt, false);
}

I want to just have the same exact results, but in C++ (cross platform). I have looked at many libraries/classes, but they don't seem resonable. First of all, Crypto++ is great and all, but all I want is RSA, so having all of their hashing/encryption/whatever else algorithms is just a waste as I look at it. I found this class, which I thought was good because it is only RSA and is pretty small. The problem is, is that the parameters either aren't named the same as the C# ones, or it doesn't support it. I feel like I shouldn't have to learn what each parameter means in RSA just to implement it, as I just want to complete a simple task here. Thanks.

Upvotes: 1

Views: 915

Answers (1)

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

The properties of the RSAParameters struct are based on the Chinese Remainder Algorithm. Have you looked at the OpenSSL crypto library?

The RSA structure consists of several BIGNUM components. It can contain public as well as private RSA keys:

struct
    {
    BIGNUM *n;              // public modulus
    BIGNUM *e;              // public exponent
    BIGNUM *d;              // private exponent
    BIGNUM *p;              // secret prime factor
    BIGNUM *q;              // secret prime factor
    BIGNUM *dmp1;           // d mod (p-1)
    BIGNUM *dmq1;           // d mod (q-1)
    BIGNUM *iqmp;           // q^-1 mod p
    // ...
    };
RSA

Upvotes: 2

Related Questions