webguosai / cryptojs-aes
php aes cbc加解密
v2.0.0
2024-08-27 06:18 UTC
Requires
- php: >=5.6
- ext-json: *
- ext-openssl: *
This package is auto-updated.
Last update: 2024-11-27 10:10:48 UTC
README
运行环境
- php >= 5.6
- composer
安装
$ composer require webguosai/cryptojs-aes
在php中使用
use \Webguosai\Aes; $array = [ 'name1' => '123', 'name2' => '456' ]; $key = 'key4567890123456'; $iv = 'iv34567890123456'; //加密 $encode = Aes::encrypt($array, $key, $iv); // dc530b22204d3ee7ce729062600fb5c389c43ededed5e5c12d22b82a791fc15e //解密 $data = Aes::decrypt($encode, $key, $iv); // ['name1' => '123', 'name2' => '456']
在JS中使用
npm install crypto-js
- crypto-js@4.2.0
<script type="text/javascript" src="node_modules/crypto-js/crypto-js.js"></script> <script> const key = 'key4567890123456' const iv = 'iv34567890123456' const keySize = 128 // 填充key,对照(128=16位,192=24位,256=32位) const padKey = key.padEnd(keySize / 8, '\0') // 填充iv(固定16位) const padIv = iv.padEnd(16, '\0') // 加密 function encrypt(data) { data = JSON.stringify(data) const encrypt = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(padKey), { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: CryptoJS.enc.Utf8.parse(padIv) }) return encrypt.ciphertext.toString(CryptoJS.enc.Hex); } // 解密 function decrypt(encrypt) { encrypt = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(encrypt)); const decrypted = CryptoJS.AES.decrypt(encrypt, CryptoJS.enc.Utf8.parse(padKey), { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: CryptoJS.enc.Utf8.parse(padIv) }) return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8)) } </script>
打赏
License
MIT