默认加解密方式
'use strict';
const express = require('express')
const bodyParser = require('body-parser')
var crypto = require('crypto');
var NodeRSA = require('node-rsa');
// 创建服务器
const app = express()
// 初始化 bodyParser
app.use(bodyParser.urlencoded({ extend: false }));
app.use(bodyParser.json());
// aes 加密解密
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes-128-cbc', key);
var crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes-128-cbc', key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// rsa 加密解密
const key = new NodeRSA({ b: 512 }) // 512 密钥长度
key.setOptions({ encryptionScheme: 'pkcs1' }) // 指定加密格式
//var publicDer = key.exportKey('public') // 公钥
//var privateDer = key.exportKey('private') // 私钥
function rsaEncrypt(data) {
return key.encrypt(data, 'base64')
}
function rsaDecrypt(data) {
return key.decrypt(data, 'utf8')
}
// 测试加密
app.post('/aesEncrypt', async (req, res) => {
res.send(aesEncrypt(req.body.data, req.body.key))
})
app.post('/rsaEncrypt', async (req, res) => {
res.send(rsaEncrypt(req.body.data))
})
// 测试解密
app.post('/aesDecrypt', async (req, res) => {
res.send(aesDecrypt(req.body.data, req.body.key))
})
app.post('/rsaDecrypt', async (req, res) => {
res.send(rsaDecrypt(req.body.data))
})
// 处理 404 页面
app.use((req, res, next) => {
res.status(404).send('404 not found')
})
// 监听端口
app.listen(3000, () => {
console.log('server start ...')
})
效果图
aes:
rsa:
使用已知的密钥加解密(RSA)
var rsaPublicKey = new NodeRSA(
'-----BEGIN PUBLIC KEY-----\n' +
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAL+JFgh+DyMJWWHNHaGgDyS6PWIMnR9w\n' +
'6U4qKPiJX9YcNTQx7wD7SsM0/jZj0nQOhyvhfy+rj3qG97erfsQLa9cCAwEAAQ==\n' +
'-----END PUBLIC KEY-----'
)
var rsaPrviteKey = new NodeRSA(
'-----BEGIN RSA PRIVATE KEY-----\n' +
'MIIBOwIBAAJBAL+JFgh+DyMJWWHNHaGgDyS6PWIMnR9w6U4qKPiJX9YcNTQx7wD7\n' +
'SsM0/jZj0nQOhyvhfy+rj3qG97erfsQLa9cCAwEAAQJAAXPXr+7MzJY4kE5O6Dff\n' +
'hYbICmNQGbnwewBE2UWpbWU7+gxRuuLA3N2AgrITC+tFUbp/dvjzkXxehZzQD7RD\n' +
'yQIhAP4+yjKPUqQ/AbmgsITyZkQ7eB3epIkYJ1O7OM3AcwxbAiEAwNt/i2efgiQ0\n' +
'np+zb4F+apn4ecxvT01JfNk/3Y+IJzUCIBOeQrnuB0F3PynUtWo8zhlbwWQEpwuR\n' +
'Go/CdZqiy/3bAiEAiPygiQ3OwCslcTKwythpSiEZ3LH4spSMSXErKx5jsK0CIQCD\n' +
'2ZrIVjgWLwNCsHtAreZmiyyPmflqcKuxd9VbiRhwig==\n' +
'-----END RSA PRIVATE KEY-----'
)
const encrypted = rsaPublicKey.encrypt('123456', 'base64')
console.log(encrypted)
const decrypted = rsaPrviteKey.decrypt(encrypted, 'utf8')
console.log(decrypted)
补充:crypto-js 的 aes 加解密
// aes js 加密
function aesEncryptJs(data, key){
const cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
iv: "",
});
const resultCipher = cipher.ciphertext.toString(CryptoJS.enc.Base64);
return resultCipher;
}
// aes js 解密
function aesDecryptJs(encrypted, key){
const decipher = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
iv: "",
});
const resultDecipher = CryptoJS.enc.Utf8.stringify(decipher);
return resultDecipher;
}