Gerharddc
Gerharddc

Reputation: 4089

C++ RSA encrypt/decrypt with XML key

I need a C++ program running on my Linux server to communicate with my C# application through RSA encrypted messages. The C# part is easy through RSACryptoServiceProvider but it only supports keys in XML format like:

Private Key

<RSAKeyValue>
    <Modulus>jhU5nGfGNopA6bHb6nxqTj3/AvPsyZnep6BTSMFyCZLtP4VRu9a/xFaYH6M6SBSf8Aod8Ljjz5aWtet9DPujC1tdG2AmDpkn/6TiUShYbYFNueB25IJoGmNfZdJl3XEWQS0pdrq0wqwgFG4GE3l8gMV6Y/gEnqBx/HZmZOv4JlbkTaQX6KIB8Sxc1zqiAQDV1uTHORG9d6Rh/Rrrv+XntGbKfx75cqU2Q6Ie+o7QoXBSkiRUgb7VlP3NlwBW9VQ03rENpovHhwLvqHbOlV90HPbl0d/JUqmBPTRLe+P+iCLPt76WE1k2Nb9mQzs8fbJ+jrfXtSqSUieeG9B+uz7pxw==</Modulus>
    <Exponent>EQ==</Exponent>
    <P>qP5inK0zFPNqViqpu1dTIrvtD2Ep29YKlnDdnYEwlRjf0VCn/IMfC4lMggOvqmh7FifwJ+GYo+9PLY6pDo5+EAjE1Lna1GRFyrd3smO9scUbAf0Zq2eqhsJjbVBdBQFX2LnARBhpVvevF7wrMpvCYhC6KmhzGrjfmhadDSSfmnU=</P>
    <Q>1zv1p5pWfN3208zp7lIDJPmLa5tO+LEMUsKg4qPFjUif2uI1++HWFCjT6OS9PS48FXv2xdWA/P3jOTw7DSK7SJX5HVBl6T6QV87jkqUtyYFxLAAVxRpGGDy6f+lVs+NyTjmQ7s2zk+Qm7DzGiUoe6BeMU0jXr/KZ4WmvOiy908s=</Q>
    <DP>T4bE/m+fkWN9Vbm5SRoJAUlgf7VA3+xBN7ykhlrpr5M8JkQS0S6lMpr208V/17iyZMeAEsSEEOkWM45trH9KYeYCRf0brz4+9fv8F7Z3YrcbxLNXX7hQP2qJJGINqAChz2Z4mIP1Vho0R2eb25SXta2EyKmflBrDk85oBi9aKpE=</DP>
    <DQ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mQzs8fbJ+jrfXtSqSUieeG9B+uz7pxw==</Modulus>
    <Exponent>EQ==</Exponent>
</RSAKeyValue>

I therefore need a C++ (gcc) RSA cryptography library that supports these keys and works on Linux. I found one library but it seems to be using a very peculiar standard where key length is measured in digits instead of bits and the only difference between its keys is the exponent while the C# standard has the exponent as being the same value in both keys.

Anyone know of a library that I can use?

Upvotes: 2

Views: 1899

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93948

RSA is a standardized algorithm. That is, actually you have RSA as defined in PKCS#1 v1.5 and there is RSA OAEP defined for encryption. So you need to synchronize the exact RSA algorithm used by a library. Some libraries also support "raw" RSA, that is: only the modular exponentiation part, but that is insecure. Note that for RSA the result is defined as octet string (aka byte array) which makes it very inter-operable.

The library that you point to seems to be a one man project hosted on Google code. That does not seem to be promising, I would stick to libraries such as OpenSSL, CryptoPP or Botan which have survived for some time in the field and have an active community.

You can simply extract the numbers from the XML and use them for any library. You do not strictly need all the parameters, just providing the Modulus, Exponent and D value should be enough. Note that Exponent in above is just the public exponent and D is the private exponent. The public exponent is normally included in an RSA private key (and can actually be used for internal validation by a crypto library). Beware that different libraries may use different notations.

Note that instead of relying on RSA directly you could also use a container format such as CMS (aka PKCS#7).

Upvotes: 1

Related Questions