最近在跟客戶的其他供應(yīng)商對接微信平臺筋夏,我們公司的微信平臺使用node搭建的蒂胞,對方未知,對方平臺內(nèi)向外跳轉(zhuǎn)加一個額外的加密token參數(shù)条篷,客戶方希望我們拿到參數(shù)后骗随,能進(jìn)行進(jìn)一步的銜接操作和處理,所以今天在這里我會把自己在已經(jīng)拿到私鑰的情況下如何在node下RSA解密赴叹,
一? 選擇模塊node-rsa
????????首先當(dāng)然是去尋找node和rsa相關(guān)的模塊鸿染,找到ursa和node-rsa等等;
????????因為多次安裝ursa失敗乞巧,度娘牡昆,google也沒得到解決,所以我這里選擇了node-rsa
cnpm install node-rsa --save
二 然后就是引入模塊摊欠,進(jìn)行簡單粗暴的解密了,這是jiami.js
//這邊是解密的function柱宦;? encrypted 參數(shù)為已經(jīng)加密的字符串
exports.jiemi=function(encrypted) {
//首先是把私鑰引入些椒,可以通過fs模塊讀取,也可以像我這樣粗暴的植入掸刊,這是用了ES6的字符串模板··;ES6的字符串模板內(nèi)是可以保留空格和換行的免糕,因為網(wǎng)上案例大多都加了/n換行拼接,我這就直接效仿了
let privatePem =`-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDQmD8Mu1G7X3XT vkUWizjYPMw0jxZjMAeNNT2G3t4vGJyl/qyYBwpIy28QlHQzUXUK5JroX4Sd8NjU 823dGSgfzEL0j9WqsuIQbXLCFuPxCezgahCfzjTFRcat4jKXE2IbjelHP/rRmpp5 bUh/MJ/wwsuoHBwnAs8MAGNwawU7LJYsyH6b/hfQCAIcuww7L5fXj2f5RDeDDse9 O3x1PMxIzZGYDkDe3X1cRabGjZvEGdKkOIulcoPUGCeuxyaokWTwk0/xYi4nHRO/ y8O574MjTDLC23XBPOaRcBzGZ9I2816iDspQay/MKdoQySQ6N4S9dRpl59oVgwgO bFEJFBlvAgMBAAECggEADnghF37+4mJyx+dQ34l0vfsppxOt19+DBpaE2BAplVa9 tv39duY2HmqOH7ZMgSrL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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());
//轉(zhuǎn)為json對象,并返回給函數(shù)
return JSON.parse(decrypted)
}
一下是調(diào)用
let jiami = require('../encrypt/jiami');
let encrypted = `qAUM6fzgnkEtYxwqv/S08xEZ7olHY7FkWXle93cMkJD1PwMvs7PRof158PQpbkVLUBf/Xz6Rgz/N9aEHZaQ+viYEFTE2DQfXhd/ZV8BxleTzxcOl2DYZoX+xzor9Wxgn3M32eY/v5VqppwRNbrSuiW54O/GFBJ7U+pDOi6CitUqrybxHrzwwFQV/OxutAmDJUgFs9gGZaBSIB3qGoDBzpQtUldUuDChW/+kH4bQPUjgClABQRxRFMrYiyvtRzOL2QQn3cQDx2Ni1AGXWd/XXCIcRjkU6tlmkDSwLapW1fo0ul7DKW38pjDVPxF7MMrySHYYUdJfzejJSNsjT/9x4rQ==`;
jiami.jiemi(encrypted);
得到的結(jié)果為