chrisa / node-rsa

OpenSSL RSA library for nodejs
Other
44 stars 9 forks source link

keypair.encrypt() Error: Can't encrypt, no public key #9

Closed mgcrea closed 12 years ago

mgcrea commented 12 years ago

Here is what i'm using (took from test).

var options = {
    passphrase: 'pocus',
    privateKey: fs.readFileSync(__dirname + '/certificates/player.key', 'ascii'),
    publicKey: fs.readFileSync(__dirname + '/certificates/player.crt', 'ascii')
};
console.log(options.publicKey); // correctly get the certificate
var keypair = rsa.createRsaKeypair(options);

var plaintext = "The Plaintext";
var ciphertext = keypair.encrypt(plaintext, 'ascii', 'base64'); // crash with "Error: Can't encrypt, no public key"
var plaintext_again = keypair.decrypt(ciphertext, 'base64', 'ascii');
assert.equal(plaintext, plaintext_again);

Using node 0.6.18 on MacOSX 10.7.

My certificate looks like this :

-----BEGIN CERTIFICATE-----
MIIC6jCCAdICCQDnJ1fXoXR/LjANBgkqhkiG9w0BAQUFADA3MQswCQYDVQQGEwJG
UjEWMBQGA1UECAwNSWxlLWRlLUZyYW5jZTEQMA4GA1UECgwHQ2FybGlwYTAeFw0x
MjA2MDYxNDM1NTBaFw0xMjA3MDYxNDM1NTBaMDcxCzAJBgNVBAYTAkZSMRYwFAYD
VQQIDA1JbGUtZGUtRnJhbmNlMRAwDgYDVQQKDAdDYXJsaXBhMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7DnRoP8XlMcxR4+bDcVunxXxQyKypq50sT0X
NoEB2b5AngRt/pzeD7gp9upgY7/9+oErVBBrunzEll9T6YdvOjxucFcPOL+mHBsn
YdjQ7Fff6FborDlr+bSvvc2UZaMV+JLemm7LZn25wDO/jcoSY2O17WzHDY8NOelf
r2B+4ZhVrRLBIZABOXuk8f61Gbw7WMKLJ7X3ZdFemrhK8DLkAWl/d5xZfuGXTTKN
KZOuSHQB5/+8iS43F3TDKFIpWfXve7hqafgpduB71GjRgBWMPHtcKayaCxSteWgL
V1Y+M6pjW9PnXl0YDWWKVzqDh4qN5/gY9I8/S6Wig04cf14fLwIDAQABMA0GCSqG
SIb3DQEBBQUAA4IBAQBldRiywKa4u2Ta5Q8/MWQQw1gEhLGzcsqR3yhU5+oj/eXU
/cknPrrvlrtdat4bx+GTe2pHwswdDbn0WTFSCZDE59rwi0KBA81YMsYZdyyfwuX2
N9IXZKjd0Ngb9sMqng9BMOufWNRIpTa1ftg+eYxz77DumOvUOQfqRpjtX74sPGGS
F3PtfQh7JgUptkg4oJLvnCmktxNdoGAz4ofFUQkvoZ6Y8IjPllRwPli1Kp65D9Ox
gmw2rZ1ePAOq8wBYRYzvmxpSfjCYoASMM/xhl+w2phwvMFG1HpJGjhGQY8ueLTZr
M4pNUxnZUVYLwIYIdXGI/ODlEFWJEZGkYY1DKAaa
-----END CERTIFICATE-----

Thanks!

mgcrea commented 12 years ago

Hum... Ok obviously i'm providing a certificate while it is waiting for a public key! Ok it worked as expected, using my own key i get :

Error: RSA decrypt: error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error

But it is another story...