JoshKaufman / ursa

URSA - RSA public/private key OpenSSL bindings for Node.js
Other
618 stars 135 forks source link

Can't decrypt generated strings #127

Closed vezaynk closed 8 years ago

vezaynk commented 8 years ago

Using my public key, I generated the letter "a" to be:

EYakv0D9+zM8zTCcQfLI6XBv2ciLEM7sHECNVR2rl0sP4imxyjtYuOUbszxKbYy+guvxKXuU9LTUEQCMoA3ddWhzRdctuREttdUKiKYz++ZO7m3m0/1utnMArjAP1WkN+k/Z7uvjQSQwfccj1uMGxNb8OsFFqM2jy/ZZK0toIfo14ReIj5AWsUhYrztO0GupfNRE57zU8zQgODxnOm+UY6r/rFu5ZcmHuIYzZseepezjxc66BGonmAnhGKXcllwOFp3rvBzSOdXB+XXG5WWgRp5rcavyEjVFOaXgD0rCW28Ie2DlWENJQ2+l/FAGRm9oRTjH7py+bm7319Ob4fgKQw==

However when I try to decrypt it with

return key.decrypt(str, 'base64', 'utf8');

I get this:

return encodeBuffer(rsa.publicEncrypt(buf, padding), outEncoding);
                                ^

Error: error:040A006E:rsa routines:RSA_padding_add_PKCS1_OAEP_mgf1:data too large for key size
    at Error (native)
    at Object.encrypt (/home/admin/cdj-messenger/node_modules/ursa/lib/ursa.js:278:33)
    at RSAencrypt (/home/admin/cdj-messenger/index.js:31:14)
    at Object.<anonymous> (/home/admin/cdj-messenger/index.js:37:13)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:136:18)
davedoesdev commented 8 years ago

@viruzx this works for me:

var ursa = require('ursa'),
    key = ursa.generatePrivateKey(),
    msg = key.encrypt('a', 'utf8', 'base64');

console.log(key.decrypt(msg, 'base64', 'utf8'));

Could you paste your code?

vezaynk commented 8 years ago

Hello. I don't remember the full context of my bug since it was so long ago, but I do remember that I was doing it completely wrong.

I was trying to keep manage large data with rsa instead of aes.

Closing.