Sunday, 15 April 2012

node.js - node-rsa and openssl compatibility -


I have a set of public / private keys that encrypt some data / for both encryption and decryption.

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

Example: Scanio:

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

  # / usr / NodeRSA = is required on bin / env node ('node rsa'); Var FS = Requirement ('FS'); Function createUsingPubKey (pub, data) {var pk = new NodeRSA (); Pk.importKey (pub); Encrypted = pk.encrypt (data, 'base64'); Encrypted return; } Var sampledata = "SECRET STUFF"; Var genkey = createUsingPubKey (fs.readFileSync ('id_rsa.pub'), sampledata) console.log (junkie);  

b) Then I try to decrypt it using the openssl utility:

  node test.js | Openssl base64 -d-A | I get:  
  RSA operation error 1068: Error: 0406706C: RSA routine: RSA_EAY_PUBLIC_DECRYPT: Data over modern lane :. \ Crypto \ rsa \ rsa_eay.c: 680:  

I thought that they would probably use different algorithms for encryption / decryption process so I led the node RSA documentation here: And I found this option:

Encryption Scheme - The padding scheme 'Pkcs1_oaep' or 'pkcs1' for encrypt / decrypt might be the default 'pkcs1_oaep'

I would like to decrypt passing the -oaep option in ut openssl like this Tried area :.

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

but I still get:

  RSA operation error 5216: error: 0406706C: RSA routine: RSA_EAY_PUBLIC_DECRYPT: data More than Mod len:. Cryptto \ rsa \ rsa_eay.c: 680:  

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

1: node .js module can be found here:

2: According to the request of Martin Bodves, here are some sample data:

The piletext data is to be encrypted:

You are not good, you are not good, you are not a baby, you are not good (I am doing I say it again) You are not good, you are not good, you are not good kids, you are not good

generated with a private key

  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 corresponding service key

  openssl RSA in key -put & gt; Key.pub ----- Start the public key ----- MFwwDQYSKIZIhvcNAQEBBQADSwAwSAJBALOUBygyX11BsDoEIKoZzn2 / HAXPorNR / X8wCDaBlcPtOHxKAZFkVra1 + Pem1urtSlnEqc07DwAP6v0GEGHpxbkCAwEAAQ == ----- END Public Key -----  

Node .js up encrypted plaintext data with the public key using RSA Library:

  nbp1tBlcev9PvD3xDmEQkcBd8ewNxW8Xm7oZWVcsFika3dU / H3VFoxdeH75DPy4 / xLvN1Gxqfb / bXTnfKZLZBYV1q4XfmR4p3ji41MAybpMiEl5h4fSFYacg5SiQ / KxxmQr0SLs4rttwcbaGBLG6rcIU + 6SSYBYu1GhC + XBlBG94zbqFV9ZvohEbnlnqDDW1Kg9hGT9 / vBBtiLEQnTiKDwztIgY3DhqadsVW0g37PFFwuQKXtHw / lQqrRhc + Pb03g + Oq8nIpX8eaurL8lo3lZNkhlY4NfFCYwP7v12MYwSrMeWPMe20LDDQ6NXbJnrsLGl5x08aYn7liS5qsYdtqRpYv + JbJc3EoXIZEyHv17gU1R0OmLsSd / Teln9VAvM + jt4jwQjlvE1WF8g9Qc / WN o28RR4KaNOvUpLDwfuc3gTgkG90ac8EchmKB3LAgU47kQComyphuPAI / G4phqeXOeYnbBrB1aqwxAkAwOIvanGjCY6FXlV8Cve1jao0ejQ0EFE1180yjhltgh5U2EErQLDd5S4y5YLbLz4xIiKo3k06Yktk4dSJsBalHPxi7Z + kofjT3KdeHyGMynodGzOmH5CaAAS2enZpp2VytcawlDu84EvrPYIRPWah9cA6dtxARx6us8EytrNIDv7UVGXw / cQEPR1nZamz6HROqT4Fpwfc =  

Just add -decrypt argument for openssl < / P>

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

Works great for me.


No comments:

Post a Comment