Ahmad Kazi
Ahmad Kazi

Reputation: 23

What is the RSA Algorithm used by C# by default, and what is it's appropriate parameter in Crypto++?

I am studying encryption and during that process, I stumbled upon this C# Code..

//Encrypts a string with RSA public key
public static string EncryptTextRSA(string text, int keySize, string publicKeyXml)
{
    var encrypted = RSAEncrypt(Encoding.UTF8.GetBytes(text), keySize, publicKeyXml);
    return Convert.ToBase64String(encrypted);
}

//Rsa encryption algorithm
public static byte[] RSAEncrypt(byte[] data, int keySize, string publicKeyXml)
{
    using (var provider = new RSACryptoServiceProvider(keySize))
    {
        provider.FromXmlString(publicKeyXml);
        return provider.Encrypt(data, OAEP);
    }
}

The information derived (from the PHP Backend) ends up like this:

Private Key:

<RSAKeyValue>
   <Modulus>3+A8GT1MAdf+MzIZGbGa5GSAjtvxsIeJnsZKeIP6oiK76TiTQqr6vgYKpE+jQBdI6WMsWv2H9TYWv6NmfzMI9+RHV82V5r71vG5o0GwvfzHj3I/FzYwvM8vjXiHIO1T4kD4ky4VNTtq29sW82pJeo4N50EgiWNvnczYKoG8PUaQIoW7j7kuDhgLtWx40zTwR6kxiqJm80oJRyLgxZEkH9YSK/rsGGtKmkOSXpMIAaJoDtBExqYtiBD6v4juAHejdqgUa1xuWLvgvdJhjGEP0kU1C2sye1QYUXN2el2+XU/N3qhsS2g4wn4VBWYNOm2Heq/rDUmkPLRcrRXzYTlVmXQ==</Modulus>
   <Exponent>AQAB</Exponent>
   <P>8Iu+4wMdHYmpzOvujzsnOCjQxRKPZxFnw9qDG1xf2/h727kfDyr2FBCXvGNHTLuRzwbc++/nUmdyvEh2uhAYlTU8eB/IcTiSzDeUWye/Bq6CL6bHR3f65bIK4K0aqFyq8eDtIEzaU0y1TxXnp6Sosk6sqp28keE1C2acnIaXQpM=</P>
   <Q>7kJRhudKzuo33N5hoTYz6rhvU++l5gdFP6RHRoGW6qBjVlxIzptUE+fYVZIvvyYQDewccKVEOb4s8etjL98lfJ85tHCxengDou9hsNh0FERM65QYN8iPXLAU39Ubca5m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iSlSEkR69o6UcjOkSi3B3dmtmFktGb0D36/yYneNES0Agca+UHWPn8triIMw4EYDuo8auMQCGR3gGg/MiJrW+Bo2gvZCoLxkAdVgoSl1e20LB1TH6UtGOs0ioMyuwG8TJSYcJVbvZZI+aZDW2YR6nrrBqcWa6n/KlLXWfbDiRq1rkmQpiIwpDRbiHOB+rao2Q93g++MYHtM0IDGWfvnWXRgXYcptMVTvzhvcdzdFwL6taa5LDAhs2HzFEawpk7EM5MpBlg1bQjCeWImIlWw==

I have been attempting to implement the same with Crypto++ in Visual Studio 2014, with issues ranging from the lack of automatic XML parsing and the general ability to import a pre-generated modulus and exponent.

My C++ Code is as such:

std::string base64ToHex(std::string base64String)
{
    std::string decodedString, finalString;
    CryptoPP::StringSource river(base64String, true,
        new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decodedString)));

    CryptoPP::StringSource stream(decodedString, true,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(finalString)));

    finalString.erase(std::remove(finalString.begin(), finalString.end(), '\n'), finalString.end());

    return finalString;
}    

std::string RSAEncryptString(std::string message, std::string modulus, std::string exponent)
{
    char modulusCharred[1024];
    strncpy_s(modulusCharred, base64ToHex(modulus).c_str(), sizeof(modulusCharred));
    modulusCharred[sizeof(modulusCharred) - 1] = 0;

    char exponentCharred[1024];
    strncpy_s(exponentCharred, base64ToHex(exponent).c_str(), sizeof(exponentCharred));
    exponentCharred[sizeof(exponentCharred) - 1] = 0;

    Integer n(modulusCharred);
    Integer e(exponentCharred);

    RSA::PublicKey pubKey;
    pubKey.Initialize(n, e);

    Integer m, c; m = Integer((const byte *)message.data(), message.size()); c = pubKey.ApplyFunction(m);
    std::stringstream ssr; ssr << hex << c;

    return (ssr.str());
}

Generated ciphertext (by C++ Implementation, using different private key):

hXM7DAe/5tymIzEsQgNGiiCnA1p/J1x7Tvu6Lhbv6ApkGwfXuDi4bXZhknLDmbTXkoMVrHKnHZE6X1+pAi9+4SxN2sVcZ/cEQx+6riBsYZrSOVqq9bzEOD8J85FIUIbWZmp8kiKBCzK7F9Ke+q5/x9aOHh29Bq7cFu//

When attempting to decrypt the cipher text (with it's corresponding private key, it does not recognise it, unlike the C# example, where it does)

The code is based off of Crypto++'s RAW RSA Encryption Wiki, what is the algorithm used by default in C# and how can that be replicated in Crypto++ (encrypting in the same algorithm as the one used in C# with the Public Key - I'm not able to pull that from the backend just yet)

Upvotes: 2

Views: 1253

Answers (1)

bartonjs
bartonjs

Reputation: 33148

The OAEP value isn't visible in your code sample, so I can't answer without a conditional.

If OAEP is false then the C# code is doing RSAES-PKCS1-v1_5. The CryptoPP equivalent is RSAES_PKCS1v15_Encryptor/RSAES_PKCS1v15_Decryptor.

If OAEP is true then the C# code is doing RSAES-OAEP with SHA-1 as the PRF. The CryptoPP equivalent is RSAES_OAEP_SHA_Encryptor/RSAES_OAEP_SHA_Decryptor (see the example).

Your code seems to be doing raw RSA. Raw RSA is dangerous (https://rdist.root.org/2009/10/06/why-rsa-encryption-padding-is-critical/, many others). The libraries that expose it are mainly trying to not be in your way if some other padding scheme were to be invented / specified on a protocol you are conforming to.

.NET does not expose raw RSA.

Upvotes: 1

Related Questions