AES加解密流程:
- 密钥
对称加密算法与非对称加密算法的区别
- 对称加密算法
- 非对称加密算法
像https就是用非对称密钥协商出对称密钥,再用对称密钥来加密业务数据
如:
利用node.js或者webpack plugin 在打包时,进行js文件内容的加密打包处理
移动端执行逻辑
const crypto = require("crypto");
const CryptoJs = require('crypto-js')
// 哈希算法
const hash = crypto.createHash("md5");
// hmac算法
const hmac = crypto.createHmac("sha256", "key");
hmac.update("hello world");
console.log(hmac.digest("hex"));
hash.update("hello world");
console.log(hash.digest("hex"));
// AES对称加密算法
// encrypt
let cipherText = CryptoJs.AES.encrypt('hello world', 'key123').toString()
console.log(cipherText)
console.log('-----加密串-----')
const bytes = CryptoJs.AES.decrypt(cipherText, 'key123')
const originalText = bytes.toString(CryptoJs.enc.Utf8)
console.log(originalText)
console.log('------解密串-----')
PS:
在前端常见的加密方式中AES很少使用,因为相对于非对称的RSA安全性很低,AES是典型的对称加密,密钥就在前端源码里(注意在信息保护中,完整性远远比机密性更重要。如果得到的信息是篡改、重放的,那你机密性没有多大用处)
https基本原理,也是用非对称密钥协商出对称密钥,再用对称密钥来加密业务数据