0

I want to decrypt my C++ Openssl RSA message with node-rsa.

In node-rsa I get

Error: Error during decryption (probably incorrect key). Original error: Error: Incorrect data or key

Encryption:

//ADD PRIVATE KEY
const char* mKey = "...";

BIO* bo = BIO_new(BIO_s_mem());

BIO_write(bo, mKey, strlen(mKey));

EVP_PKEY* pKey = 0;
PEM_read_bio_PrivateKey(bo, &pKey, 0, 0);

BIO_free(bo);

RSA* keypair = EVP_PKEY_get1_RSA(pKey);

char msg[2048 / 8] = "Hallo Welt!";
std::cout << "Message to encrypt: " << msg << std::endl;


//ENCRYPT
char* encrypt = (char*)malloc(RSA_size(keypair));
int encrypt_len;

if ((encrypt_len = RSA_public_encrypt(strlen(msg) + 1, (unsigned char*)msg, (unsigned char*)encrypt, keypair, RSA_PKCS1_PADDING)) == -1) {
    std::cout << "ERROR";
}

std::cout << "ENCRYPTED: " << std::endl;
std::cout << base64_encode(encrypt);
std::cout << std::endl;

Result:

Message to encrypt: Hallo Welt!
ENCRYPTED:
xR00Ei3kxS4SYJfBlGX8IPyyBbOAnAOqwfdKtzJNs4JsKo/5g1IhCesY0YETyJX9S48uWNWHr5C3IAGu2+0N0qdi8WDs+m+oX/F1I7H5/n8pQcwqGn81mhSe6cYpBFHdX2GgzuJ8tNr3HsigorA=

Now the node-rsa code which doesn't work:

const NodeRSA = require('node-rsa');

const key = new NodeRSA(...);

key.setOptions({encryptionScheme: 'pkcs1'});

const text = key.decrypt("xR00Ei3kxS4SYJfBlGX8IPyyBbOAnAOqwfdKtzJNs4JsKo/5g1IhCesY0YETyJX9S48uWNWHr5C3IAGu2+0N0qdi8WDs+m+oX/F1I7H5/n8pQcwqGn81mhSe6cYpBFHdX2GgzuJ8tNr3HsigorA=", 'utf8');

console.log(text);

Used Key:

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA2vyUeV4ODTjVZi+z04q94rJfYx7u54HbPrsc94K6e90hYcw4
G5envqpJLU7NRrQP0UOGAkM0+7xPyjHVzfu3I5b+DL3BE2d1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oxRDSvOkkmyUvkmFrkqg93XRFXVRAvysy
IREhHBmAGnUqe34ogKVn8QKBgAE92idA0Dn1m4BV4NkEtDnHhjhs1NnxNw2BAH5P
Vi8T3MEKObVZc1MOqwkMlYQeBrL+fqkaqNSLVb5uLSyZkgJcU4/CU5GgKhFA0gwp
qNg/DQeEyejw5OcOMBz4tVFVpZJHTDUqRgAWYv//m4smZqOvlYOm3B1uGgrLLo/f
sbidAoGBAKkDXurjcUBPWWGvprkGXx5WXIepn1hXIUtfCytwiwG1kixgSP+wwsE/
85klHhvXqLAmDE9VDJwDna62p6uEaGyjyB6lka0hFKzKXQB5YhxoM08va2aXRaVB
9eNlIzlbZItG85c8ZTtlrK74IBy3JTirBB5yJvXV4o0x2kZhEwhU
-----END RSA PRIVATE KEY-----

I don't understand what I am doeing wrong. Is it possible to de/encrypt with these different systems?

5
  • You use a 2048 bit = 256 bytes key, i.e. the ciphertext should also be 256 bytes long. But it has Base64 decoded only a length of 110 bytes. So there seems to be a problem with the encryption. Commented Feb 27, 2021 at 14:58
  • That could be but I also tried it to decrypt it as a "normal text" from a text-file with the openssl bash. Didn't worked... I've also encrypted and decrypted it with the bash and the node-rsa library. That worked perfectly! Only C++ is doeing something different. Commented Feb 27, 2021 at 16:27
  • @Topaco but now as you say it. In the bash I get "data greater than mod len" Commented Feb 27, 2021 at 16:42
  • According to this error message your plaintext is longer than the allowed length. With RSA and PKCS#1 v1.5 padding, for a 2048 bits = 256 bytes key the maximum allowed plaintext length is 245 bytes, here. For longer messages hybrid encryption is applied. Commented Feb 27, 2021 at 17:07
  • @Topaco But my message isn't so long. I have a 1024 bit private key and my plaintext is "Hallo Welt!"... And the en/decryption is working with the bash and node-rsa. I am slowly despairing... Commented Feb 27, 2021 at 21:45

0

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.