Skip to content

Commit 55a095a

Browse files
committed
added encryptPrivate, decryptPublic methods
1 parent 7420fcb commit 55a095a

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

src/NodeRSA.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,52 @@ module.exports = (function () {
221221
};
222222

223223
/**
224-
* Encrypting data method
224+
* Encrypting data method with public key
225225
*
226226
* @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
227227
* @param encoding {string} - optional. Encoding for output result, may be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
228228
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
229229
* @returns {string|Buffer}
230230
*/
231231
NodeRSA.prototype.encrypt = function (buffer, encoding, source_encoding) {
232+
return this.$$encryptKey(false, buffer, encoding, source_encoding);
233+
};
234+
235+
/**
236+
* Decrypting data method with private key
237+
*
238+
* @param buffer {Buffer} - buffer for decrypting
239+
* @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type
240+
* @returns {Buffer|object|string}
241+
*/
242+
NodeRSA.prototype.decrypt = function (buffer, encoding) {
243+
return this.$$decryptKey(false, buffer, encoding);
244+
};
245+
246+
/**
247+
* Encrypting data method with private key
248+
*
249+
* Parameters same as `encrypt` method
250+
*/
251+
NodeRSA.prototype.encryptPrivate = function (buffer, encoding, source_encoding) {
252+
return this.$$encryptKey(true, buffer, encoding, source_encoding);
253+
};
254+
255+
/**
256+
* Decrypting data method with public key
257+
*
258+
* Parameters same as `decrypt` method
259+
*/
260+
NodeRSA.prototype.decryptPublic = function (buffer, encoding) {
261+
return this.$$decryptKey(true, buffer, encoding);
262+
};
263+
264+
/**
265+
* Encrypting data method with custom key
266+
*/
267+
NodeRSA.prototype.$$encryptKey = function (usePrivate, buffer, encoding, source_encoding) {
232268
try {
233-
var res = this.keyPair.encrypt(this.$getDataForEncrypt(buffer, source_encoding));
269+
var res = this.keyPair.encrypt(this.$getDataForEncrypt(buffer, source_encoding), usePrivate);
234270

235271
if (encoding == 'buffer' || !encoding) {
236272
return res;
@@ -243,16 +279,12 @@ module.exports = (function () {
243279
};
244280

245281
/**
246-
* Decrypting data method
247-
*
248-
* @param buffer {Buffer} - buffer for decrypting
249-
* @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type
250-
* @returns {Buffer|object|string}
282+
* Decrypting data method with custom key
251283
*/
252-
NodeRSA.prototype.decrypt = function (buffer, encoding) {
284+
NodeRSA.prototype.$$decryptKey = function (usePublic, buffer, encoding) {
253285
try {
254286
buffer = _.isString(buffer) ? new Buffer(buffer, 'base64') : buffer;
255-
var res = this.keyPair.decrypt(buffer);
287+
var res = this.keyPair.decrypt(buffer, usePublic);
256288

257289
if (res === null) {
258290
throw Error('Key decrypt method returns null.');

src/libs/rsa.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ module.exports.Key = (function() {
206206
* @param buffer {Buffer}
207207
* @returns {Buffer}
208208
*/
209-
RSAKey.prototype.encrypt = function (buffer) {
209+
RSAKey.prototype.encrypt = function (buffer, usePrivate) {
210210
var buffers = [];
211211
var results = [];
212212
var bufferSize = buffer.length;
@@ -225,7 +225,7 @@ module.exports.Key = (function() {
225225
var buf = buffers[i];
226226

227227
var m = new BigInteger(this.encryptionScheme.encPad(buf));
228-
var c = this.$doPublic(m);
228+
var c = usePrivate ? this.$doPrivate(m) : this.$doPublic(m);
229229

230230
if (c === null) {
231231
return null;
@@ -243,7 +243,7 @@ module.exports.Key = (function() {
243243
* @param buffer {Buffer}
244244
* @returns {Buffer}
245245
*/
246-
RSAKey.prototype.decrypt = function (buffer) {
246+
RSAKey.prototype.decrypt = function (buffer, usePublic) {
247247
if (buffer.length % this.encryptedDataLength > 0) {
248248
throw Error('Incorrect data or key');
249249
}
@@ -258,7 +258,8 @@ module.exports.Key = (function() {
258258
length = offset + this.encryptedDataLength;
259259

260260
var c = new BigInteger(buffer.slice(offset, Math.min(length, buffer.length)));
261-
var m = this.$doPrivate(c);
261+
var m = usePublic ? this.$doPublic(c) : this.$doPrivate(c);
262+
262263
result.push(this.encryptionScheme.encUnPad(m.toBuffer(this.encryptedDataLength)));
263264
}
264265

test/tests.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ describe("NodeRSA", function(){
361361
var key = null;
362362
var suit = dataBundle[i];
363363

364-
it("should encrypt " + i, function () {
364+
it("`encrypt()` should encrypt " + i, function () {
365365
key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length];
366366
key.setOptions({encryptionScheme: scheme});
367367
encrypted[i] = key.encrypt(suit.data);
368368
assert(Buffer.isBuffer(encrypted[i]));
369369
assert(encrypted[i].length > 0);
370370
});
371371

372-
it("should decrypt " + i, function () {
372+
it("`decrypt()` should decrypt " + i, function () {
373373
decrypted[i] = key.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
374374
if (Buffer.isBuffer(decrypted[i])) {
375375
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
@@ -379,6 +379,30 @@ describe("NodeRSA", function(){
379379
});
380380
})(i);
381381
}
382+
383+
for (var i in dataBundle) {
384+
(function (i) {
385+
var key = null;
386+
var suit = dataBundle[i];
387+
388+
it("`encryptPrivate()` should encrypt " + i, function () {
389+
key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length];
390+
key.setOptions({encryptionScheme: scheme});
391+
encrypted[i] = key.encryptPrivate(suit.data);
392+
assert(Buffer.isBuffer(encrypted[i]));
393+
assert(encrypted[i].length > 0);
394+
});
395+
396+
it("`decryptPublic()` should decrypt " + i, function () {
397+
decrypted[i] = key.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
398+
if (Buffer.isBuffer(decrypted[i])) {
399+
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
400+
} else {
401+
assert(_.isEqual(suit.data, decrypted[i]));
402+
}
403+
});
404+
})(i);
405+
}
382406
});
383407

384408
describe("Bad cases", function () {

0 commit comments

Comments
 (0)