Js通过AES加密后PHP用Openssl解密的方法
作者:xialeistudio 时间:2023-07-17 08:04:42
前言
最近遇到的几个网站在提交密码时提交的已经是密文,也就是说在网络上传输的密码是密文,这样提升了密码在网络传输中的安全性。
后端语言加解密已经有很成熟的方案了,前端的话Google之前出过一个crypto-js,为浏览器的js提供了加解密方案。今天一起来了解一下基于AES的前后端加解密流程。
Javascript
1、安装npm包 npm install crypto-js
2、加密代码
const CryptoJS = require("crypto-js");
const key = CryptoJS.enc.Latin1.parse('1234567812345678');
const iv = CryptoJS.enc.Latin1.parse('1234567812345678');
const encoded = CryptoJS.AES.encrypt('hahaha', key, {
iv: iv,
mode: CryptoJS.mode.CBC,
adding: CryptoJS.pad.ZeroPadding
}).toString()
console.log('encoded', encoded)
3、解密代码
const key = CryptoJS.enc.Latin1.parse('123456781234567812345678');
const iv = CryptoJS.enc.Latin1.parse('1234567812345678');
const decoded = CryptoJS.AES.decrypt(encoded, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
adding: CryptoJS.pad.ZeroPadding
}).toString(CryptoJS.enc.Utf8)
console.log('decoded', decoded);
4、输出如下
encoded 6bcgYd4f4ZgNOQH/3tqMpg==
decoded hahaha
PHP
直接使用openssl解密即可,代码如下:
$encoded = '6bcgYd4f4ZgNOQH/3tqMpg==';
$key = '123456781234567812345678';
$iv = '1234567812345678';
var_dump(openssl_decrypt($encoded, 'AES-192-CBC', $key, 0,$iv));
输出结果:
string(6) "hahaha"
注意事项
1、AES加密位数跟密钥key有关, 以下是密钥位数和加密对应关系
16 => AES-128
24 => AES-192
32 => AES-256
2、iv是初始化向量. 超过16字节或者不足16字节都会被补足16字节或者截断到16字节。由于AES是块加密,铭文被分割成固定长度的块(一般是16字节长度),所以iv也是16字节。
3、CBC是加密模式
来源:https://www.ddhigh.com/2019/07/11/js-encrypt-php-decrypt.html
标签:js,aes加密,openssl解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python中使用元类的教程
2021-04-12 05:09:34
基于Python新建用户并产生随机密码过程解析
2021-08-26 11:25:18
Go语言中的Array、Slice、Map和Set使用详解
2023-06-24 07:29:03
详解python中docx库的安装过程
2023-01-21 18:32:36
![](https://img.aspxhome.com/file/2023/6/110036_0s.jpg)
Python中torch.norm()用法解析
2021-12-01 09:42:43
![](https://img.aspxhome.com/file/2023/9/66749_0s.png)
ASP分页函数
2009-07-06 12:41:00
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2023-07-17 09:58:42
![](https://img.aspxhome.com/file/2023/0/65540_0s.png)
JS的Form表单转JSON格式的操作代码
2023-07-02 05:24:03
Python装饰器(decorator)定义与用法详解
2022-07-21 03:11:25
ORACLE 如何查询被锁定表及如何解锁释放session
2023-07-02 11:59:39
全面详解JS正则中匹配技巧及示例
2024-03-24 15:07:39
PyTorch平方根报错的处理方案
2022-05-12 09:09:19
![](https://img.aspxhome.com/file/2023/4/95454_0s.png)
Python OpenCV图像处理之图像滤波特效详解
2021-06-07 04:36:02
![](https://img.aspxhome.com/file/2023/4/114494_0s.jpg)
SQL Server恢复模型之批量日志恢复模式
2024-01-28 07:34:22
![](https://img.aspxhome.com/file/2023/6/98036_0s.png)
python实现自动登录
2023-10-20 05:11:46
![](https://img.aspxhome.com/file/2023/7/134867_0s.jpg)
python strip()函数 介绍
2023-06-15 11:59:47
Jupyter加载文件的实现方法
2021-11-12 23:16:15
![](https://img.aspxhome.com/file/2023/8/85058_0s.jpg)
解决pandas中读取中文名称的csv文件报错的问题
2021-08-07 00:53:40
MySql5.6使用validate password 插件加强密码强度的安装及使用方法
2024-01-24 15:39:20
python查询mysql中文乱码问题
2021-09-28 07:05:42