最近在跟客户的其他供应商对接微信平台,我们公司的微信平台使用node搭建的,对方未知,对方平台内向外跳转加一个额外的加密token参数,客户方希望我们拿到参数后,能进行进一步的衔接操作和处理,所以今天在这里我会把自己在已经拿到私钥的情况下如何在node下RSA解密,
一 选择模块node-rsa
首先当然是去寻找node和rsa相关的模块,找到ursa和node-rsa等等;
因为多次安装ursa失败,度娘,google也没得到解决,所以我这里选择了node-rsa
cnpm install node-rsa --save
二 然后就是引入模块,进行简单粗暴的解密了,这是jiami.js
//这边是解密的function; encrypted 参数为已经加密的字符串
exports.jiemi=function(encrypted) {
//首先是把私钥引入,可以通过fs模块读取,也可以像我这样粗暴的植入,这是用了ES6的字符串模板··;ES6的字符串模板内是可以保留空格和换行的,因为网上案例大多都加了/n换行拼接,我这就直接效仿了
let privatePem =`-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDQmD8Mu1G7X3XT vkUWizjYPMw0jxZjMAeNNT2G3t4vGJyl/qyYBwpIy28QlHQzUXUK5JroX4Sd8NjU 823dGSgfzEL0j9WqsuIQbXLCFuPxCezgahCfzjTFRcat4jKXE2IbjelHP/rRmpp5 bUh/MJ/wwsuoHBwnAs8MAGNwawU7LJYsyH6b/hfQCAIcuww7L5fXj2f5RDeDDse9 O3x1PMxIzZGYDkDe3X1cRabGjZvEGdKkOIulcoPUGCeuxyaokWTwk0/xYi4nHRO/ y8O574MjTDLC23XBPOaRcBzGZ9I2816iDspQay/MKdoQySQ6N4S9dRpl59oVgwgO bFEJFBlvAgMBAAECggEADnghF37+4mJyx+dQ34l0vfsppxOt19+DBpaE2BAplVa9 tv39duY2HmqOH7ZMgSrLVaAZ5riJeHM2dz8Thdyy3dHLay93AMo7yCvvY+s2kIfB uKdi09L38h8u8uzX2YbsrUCgx+J2QYfZ40ZjWv9CeO7qNZUNlt8pAYodW7TwvfPv swm8/fW/XYah9bQCFh0rd4vZPp1XQgrZaEHkjDdTQE4lnHjqoKcztvTgzQf/+Pf5 kPP+xgFNRjOzy9F+jhQW38gnJewCLdJ2Jg/pujKYCgPwffZl4bLzDBwiaqk44ZBB xB016PSILXzV7B/7JO2kARK7hoBfjujpZcAvbOkiwQKBgQD0Spn3RKSReRHl6QyB rT6FlabO4CE8BTfJo2/6eKX6Ru7XneRKEdr8CUn14zwxsVT723ERfk68KoFBXvJv GEKpIjv7V0Ou03JaQoIhuj2YN+RRXgzIY1ZOQQcjLoleCDdX90mVlxAv1xHbhpkj 2OFs7pisp36lLEKkr/KsvMNvUQKBgQDal6eZt2DiK9NWaS5iVwWXysnOdQHDxrP/ KipLPnuYQC+1qZs0UbGBilytEFLSbGwr/YNagTyZLPg1g+eBMG0W0MLWHkcJh3Bi vM40vrEA2O+t7dLaaXRZhYDSd5DEcOUPUYPhmKs9HMs3XLEc0tHRJqsQiD5K6eab iEh4bLhMvwKBgQCWdFfNJXahrcorKlQsjbGC/n9EzF9Lov8P7dvqjGHU+djh7PmL 8OFI9sOZcAFDra7kiuthVcRSOW9v/nUbnPp5Rn9gtgcWZl34Xxvcf+KtnEa/IkiH Jj8iZQRrU58AzdigEco9GlO60x/Jb2Vj4ZA80dowA9yJB/Bnk4HHh15qcQKBgDCg ef09CQVnQG1BmrrvpuPcjcYqy0i1BEeoJp50eOQmqzZbU5Zw8e7iS7mIHxSGYA3t KHJSu5tLoK+u9yoLDsl7iUw+kgjxozOj0q9zGEca+z6S4EwMF4DurPik9oRspNgi hxcBcl528FIRgajq0RgY5QcZd9oMbGt/BrgbX2hbAoGBANsh1I3bnOqPQ51EO51Z kgEwCKecy+n8PTc1eDdNK8EjAv9chxfFxlGyvYx3VF1o8ULhE0zlHObTZPBDWAiT EcEMhce6cfNWD0XJ8ulacOQmtxaMKA3w83c7ntk2feW/yrLpnzLbJ+DLfhalOTaD coI1KnJDeBSdOQ2cwW0R92Oi -----END PRIVATE KEY-----`;
let Rsa = require('node-rsa');
//下面是通过fs模块读取私钥的方式,已注释,有兴趣的也可以尝试一下
//letfs = require('fs');
// var privatePem = fs.readFileSync('./rsa_private_key.pem').toString();
// var publicPem = fs.readFileSync('./rsa_public_key.pem').toString(); // var text = 'qqqqqq'; // // var public_key = new Rsa(publicPem); var private_key = new Rsa(privatePem);
//下面是初始化公钥和私钥的操作
// public_key.setOptions({encryptionScheme: 'pkcs1'});
private_key.setOptions({encryptionScheme: 'pkcs1'});
// console.log('text:', text);
//以下是加密处理
//var encrypted = public_key.encrypt(text, 'base64');
//console.log('encrypted: ', encrypted);
//以下是解密处理
var decrypted = private_key.decrypt(encrypted, 'utf8'); console.log(JSON.parse(decrypted).timeout>new Date());
//转为json对象,并返回给函数
return JSON.parse(decrypted)
}
一下是调用
let jiami = require('../encrypt/jiami');
let encrypted = `qAUM6fzgnkEtYxwqv/S08xEZ7olHY7FkWXle93cMkJD1PwMvs7PRof158PQpbkVLUBf/Xz6Rgz/N9aEHZaQ+viYEFTE2DQfXhd/ZV8BxleTzxcOl2DYZoX+xzor9Wxgn3M32eY/v5VqppwRNbrSuiW54O/GFBJ7U+pDOi6CitUqrybxHrzwwFQV/OxutAmDJUgFs9gGZaBSIB3qGoDBzpQtUldUuDChW/+kH4bQPUjgClABQRxRFMrYiyvtRzOL2QQn3cQDx2Ni1AGXWd/XXCIcRjkU6tlmkDSwLapW1fo0ul7DKW38pjDVPxF7MMrySHYYUdJfzejJSNsjT/9x4rQ==`;
jiami.jiemi(encrypted);
得到的结果为