-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypted.js
46 lines (34 loc) · 1.27 KB
/
encrypted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @Author: your name
* @Date: 2019-12-17 17:19:16
* @LastEditTime : 2020-01-14 20:43:11
* @LastEditors : Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \chatroom\encrypted.js
*/
const NodeRSA = require('node-rsa')
const {privatePem} = require('./config/privateKey')
const CryptoJS = require('crypto-js')
function aes_decrypt(ciphertext, a, b) {
var decrypted = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(a), {iv: CryptoJS.enc.Utf8.parse(b)});
return decrypted.toString(CryptoJS.enc.Utf8);
}
module.exports = (msg) => {
let msgObj = JSON.parse(msg) || '';
if (msgObj) {
const rsa= new NodeRSA(privatePem);
rsa.setOptions({encryptionScheme: 'pkcs1'});
try{
// 如果需要对AES加密的消息做一些操作,则需要通过RSA私钥解密出AES密钥,之后通过AES密钥解密出消息;
let AESKEYStr = rsa.decrypt(msgObj.encryptedAESKEY, 'utf8');
let {AES_KEY, AES_IV} = JSON.parse(AESKEYStr);
console.log(AES_KEY)
return aes_decrypt(msgObj.encrypted, AES_KEY, AES_IV);
}catch(e){
console.log(e)
console.log('incorrect key');
// 不能返回’‘,json.parse()会报错
return null;
}
}
}