@@ -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.' ) ;
0 commit comments