http协议下,如何对用户的登陆密码进行加密
目的:防止秘密明文传输
记录下现在项目用到的,通过使用crypto.js对数据进行加密
原理:通过crypto提供的aes(常用的对称加密算法,加解密都使用同一个密匙)模块实现数据的加密和解密,前端加密数据,后台解密
代码解释如下:
const crypto = require('crypto');
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes192', key);
let crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes192', key);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}