Skip to content

webguosai/cryptojs-aes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cryptojs-aes

Latest Stable Version Total Downloads Latest Unstable Version License

运行环境

  • 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

<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