`public_key` can be an object or a string. If `public_key` is a string, it is
treated as the key with no passphrase and will use `RSA_PKCS1_OAEP_PADDING`.
+Since RSA public keys may be derived from private keys you may pass a private
+key to this method.
`public_key`:
* `key` : A string holding the PEM encoded private key
+* `passphrase` : An optional string of passphrase for the private key
* `padding` : An optional padding value, one of the following:
* `constants.RSA_NO_PADDING`
* `constants.RSA_PKCS1_PADDING`
return function(options, buffer) {
var key = options.key || options;
var padding = options.padding || defaultPadding;
- return method(toBuf(key), buffer, padding);
+ var passphrase = options.passphrase || null;
+ return method(toBuf(key), buffer, padding, passphrase);
};
}
}, encryptedBuffer);
assert.equal(input, decryptedBufferWithPassword.toString());
+ encryptedBuffer = crypto.publicEncrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: 'password'
+ }, bufferToEncrypt);
+
+ decryptedBufferWithPassword = crypto.privateDecrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: 'password'
+ }, encryptedBuffer);
+ assert.equal(input, decryptedBufferWithPassword.toString());
+
+ encryptedBuffer = crypto.privateEncrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: new Buffer('password')
+ }, bufferToEncrypt);
+
+ decryptedBufferWithPassword = crypto.publicDecrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: new Buffer('password')
+ }, encryptedBuffer);
+ assert.equal(input, decryptedBufferWithPassword.toString());
+
encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
crypto.privateDecrypt({
key: rsaKeyPemEncrypted,
passphrase: 'wrong'
+ }, bufferToEncrypt);
+ });
+
+ assert.throws(function() {
+ crypto.publicEncrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: 'wrong'
+ }, encryptedBuffer);
+ });
+
+ encryptedBuffer = crypto.privateEncrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: new Buffer('password')
+ }, bufferToEncrypt);
+
+ assert.throws(function() {
+ crypto.publicDecrypt({
+ key: rsaKeyPemEncrypted,
+ passphrase: [].concat.apply([], new Buffer('password'))
}, encryptedBuffer);
});
})();