0

I am creating a C# app that authenticates via a node.js server. I am using RSA for this purpose. I generated a public and private key for the server using crypto. Every time the client connects to the server, it generates a key pair for itself. The client gets the server public key from an endpoint. I have used XML strings as well as PEM strings, but neither of them worked. (using RSACryptoServiceProvider) When the server attempted to decrypt it, it threw an OAEP decoding error. I am trying to decrypt the message with the paired private key.

I have viewed other threads but they were not very helpful.

Here's the code for the server. It encrypts/decrypts with the built-in crypto module. (I have tested this with a node.js client and a node.js server, and it works.)


var encrypt = function(input, publicKey) {
    var buffer = Buffer.from(input);
    var encrypted = crypto.publicEncrypt(publicKey, buffer);
    return encrypted.toString("base64");
};

var decrypt = function(input, privateKey) {
    var buffer = Buffer.from(input, "base64");
    var decrypted = crypto.privateDecrypt(privateKey, buffer);
    return decrypted.toString("utf8");
};

module.exports = {
    encrypt,
    decrypt
}

Edit: I made a test C# console app that takes an input string and encrypts it with my node.js server's public key.

        public const string pubKey = "<RSAKeyValue>public key etc etc</RSAKeyValue>";
        private static void Main(string[] args)
        {
            string enc = encrypt(pubKey, args[0]);
            Console.WriteLine(enc);
        }

        public static string encrypt(string publicKey, string decrypted)
        {
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
            csp.FromXmlString(publicKey);
            byte[] bytesPlainTextData = Encoding.UTF8.GetBytes(decrypted);
            byte[] bytesCipherText = csp.Encrypt(bytesPlainTextData, false);
            string cipherText = Convert.ToBase64String(bytesCipherText);
            return cipherText;
        }

It gave me the result VnzRc4yhIa9XcSDvHyDkwCNHFG6Ps2dddyCD4RHE4jIqvMl56DhmIJWprLRZle9EpZ/3Zq4fDkkplHUGBidoH+9VkPV/2+sV6P+C+4u6yisV5zTarZfjcvsShwBp/9z4YfOE7kQZVRENhvflrRw6GutxtDz0lO4KhvdvQztm0u7JmUB9ynM7XFYXOKT391InBs2eqRh+JRfJzTfhFqn3Bt8K/kKNE1xkvQV0GK7U1qSpWOWfB+0hdwNkUEQpT26jU93bAcex1SVwfbj4PJQMH6Wxzx2s6u4fcOzf9ELEgel/Fuj5b0UKHHE48B/zBmnoDsS3twt/8TJb9jbCU8S3ES/hKwndkS809bSoJl6TkBXErlOLCDpay3AO23+NjPGwSl1JvnFUVgTqAABd/yAcsokjIgxkbRqAvhC/js5Oh3y9wJwc9Z7V1ImPGcifIWsEBuH/8lerJdYw7ABB/eUZosC+tQkzvjr4H9urupM0mk6Zd+92sJaG/COrwOAPkiiM6lJK9ealRrlPMEKv39aWVr+brlQzN8zyoT+a0oGsYSPt9B/P3CJhbkbHqw9e1u9TZ7q9Ba7x/oqeRBmpRfFrcjegGFQkYViYkd1bswNF3KumqhBCsw4VeTkYmRNCKrLZdZyJ5BLSfvc+PTPOzDPVgOZb1InacmIWOqkapRbeELc=

Then, I did a simple console.log(decrypt(stringAbove, privateKey));

It still gives me the following error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

0

1 Answer 1

2

There are multiple types of padding, and apparently the encryption is trying to use PKCS1 (I guess), and the decryption defaults to OAEP.

In crypto.privateDecrypt you can set the padding to eg. padding: crypto.constants.RSA_PKCS1_PADDING and it should work.

You should go for OAEP on both ends if possible (and it should be), in which case your Node code is already ok as the default is OAEP, and C# should be set to OAEP too.

Edit: I mixed it up first, but the point is, you can set the padding type on either end, and they must match. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I changed it to OAEP and the code now works fine. :)
This is a general rule when doing cryptography across different systems. You cannot rely on defaults because different systems have different defaults. You need to set everything explicitly so you know it is the same on both sides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.