Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-rsa and openssl compatibility

I have a set of public/private keys, that works flawless when encrypting/decrypting some data using only one of the 2 ways for both encryption and decryption.

I still have no luck trying to encrypt the data with the one of the two and decrypt it with the other.

Example schenario:

a) I create some encrypted data using the public key with the following node.js code:

#!/usr/bin/env node

var NodeRSA = require('node-rsa');
var fs = require('fs');

function createUsingPubKey(Pub, data) {
    var pk = new NodeRSA();
    pk.importKey(Pub);
    encrypted = pk.encrypt(data, 'base64');
    return encrypted;
}

var sampledata = "SECRET STUFF";

var genkey = createUsingPubKey(fs.readFileSync('id_rsa.pub'), sampledata)
console.log(genkey);

b) Then i try to decrypt it using openssl utility with this:

node test.js | openssl base64 -d -A | openssl rsautl -inkey id_rsa

But i get:

RSA operation error
1068:error:0406706C:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data greater than mod len:.\crypto\rsa\rsa_eay.c:680:

I thought that they probably would use different algorithms for the encryption/decryption procedure so i headed to the node-rsa documentation here: https://www.npmjs.com/package/node-rsa and i found this option:

encryptionScheme — padding scheme for encrypt/decrypt. Can be 'pkcs1_oaep' or 'pkcs1'. Default 'pkcs1_oaep'.

I then tried to decrypt passing the -oaep option in openssl util like this:

node test.js | openssl base64 -d -A | openssl rsautl -oaep -inkey id_rsa

But i still get:

RSA operation error
5216:error:0406706C:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data greater than mod len:.\crypto\rsa\rsa_eay.c:680:

My knowledge on cryptography is really basic. Any help would be appreciated :)

EDIT 1: The node.js module can be found here: https://github.com/rzcoder/node-rsa

EDIT 2: As Maarten Bodewes requested, here is some sample data:

Plaintext data to be encrypted:

You're no good, you're no good, you're no good Baby, you're no good (I'm gonna say it again) You're no good, you're no good, you're no good Baby, you're no good

A private key generated with

openssl genrsa -out key 512

-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALOUBygyX11BsDoEIKoZzn2/HAXPorNR/X8wCDaBlcPtOHxKAZFk
Vra1+Pem1urtSlnEqc07DwAP6v0GEGHpxbkCAwEAAQJAGAZ17qrOl2tyaFClDhzl
w20OErj0y4jsoVeLwb8UimG48JslS14hfM9XxE/fG6qypN8u7LUhlnBC68ZcQ9Jg
AQIhAORaVlB7trWp6n7dETvdY9J2p8ubOuyLTX0BA2jF8agxAiEAyVHzDWQPWx/s
gt+ABErqN+ZUWS016DD34QUVGyp9nAkCIQC39JpSDcd7gx1YA8jNXCT9N/8mg6+t
PO84g2d2sPdjEQIgXwWMF/TzfopJ4tfFH8GQXYQcqd66A/cg+Jeih6j9kqkCIGD4
hBAO0haqnqeSO65Mm1IjY/6Z77pKxzJAGys5XeXk
-----END RSA PRIVATE KEY-----

And its equivalent pub key

openssl rsa -in key -pubout > key.pub

-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALOUBygyX11BsDoEIKoZzn2/HAXPorNR
/X8wCDaBlcPtOHxKAZFkVra1+Pem1urtSlnEqc07DwAP6v0GEGHpxbkCAwEAAQ==
-----END PUBLIC KEY-----

The above plaintext data encrypted with the given public key using the node.js rsa library:

nbp1tBlcev9PvD3xDmEQkcBd8ewNxW8Xm7oZWVcsFika3dU/H3VFoxdeH75DPy4/xLvN1Gxqfb/bXTnfKZLZBYV1q4XfmR4p3ji41MAybpMiEl5h4fSFYacg5SiQ/KxxmQr0SLs4rttwcbaGBLG6rcIU+6SSYBYu1GhC+XBlBG94zbqFV9ZvohEbnlnqDDW1Kg9hGT9/vBBtiLEQnTiKDwztIgY3DhqadsVW0g37PFFwuQKXtHw/lQqrRhc+Pb03g+Oq8nIpX8eaurL8lo3lZNkhlY4NfFCYwP7v12MYwSrMeWPMe20LDDQ6NXbJnrsLGl5x08aYn7liS5qsYdtqRpYv+JbJc3EoXIZEyHv17gU1R0OmLsSd/Teln9VAvM+jt4jwQjlvE1WF8g9Qc/WNo28RR4KaNOvUpLDwfuc3gTgkG90ac8EchmKB3LAgU47kQComyphuPAI/G4phqeXOeYnbBrB1aqwxAkAwOIvanGjCY6FXlV8Cve1jao0ejQ0EFE1180yjhltgh5U2EErQLDd5S4y5YLbLz4xIiKo3k06Yktk4dSJsBalHPxi7Z+kofjT3KdeHyGMynodGzOmH5CaAAS2enZpp2VytcawlDu84EvrPYIRPWah9cA6dtxARx6us8EytrNIDv7UVGXw/cQEPR1nZamz6HROqT4Fpwfc=
like image 644
Fr0stBit Avatar asked Oct 31 '22 08:10

Fr0stBit


1 Answers

Just add -decrypt argument for openssl

node test.js | openssl base64 -d -A | openssl rsautl -decrypt -oaep -inkey id_rsa

works pretty nice for me.

like image 122
RzCoDer Avatar answered Nov 15 '22 06:11

RzCoDer