js 和 php 对称加密

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://cdn.bootcss.com/crypto-js/3.1.9/crypto-js.js"></script>
<script>
const AES_KEY = "qq3217834abcdefg"; //16位
const AES_IV = "1234567890123456";  //16位

function aes_encrypt(plainText) {
    var encrypted = CryptoJS.AES.encrypt(plainText, CryptoJS.enc.Utf8.parse(AES_KEY), {iv:  CryptoJS.enc.Utf8.parse(AES_IV)});
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}

function aes_decrypt(ciphertext) {
    var decrypted = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(AES_KEY), {iv: CryptoJS.enc.Utf8.parse(AES_IV)});
    return decrypted.toString(CryptoJS.enc.Utf8);
}

data = 'my message';
encrypt_data = aes_encrypt(data);
console.log(encrypt_data);
decrypt_data = aes_decrypt(encrypt_data);
console.log(decrypt_data);
</script>
class AesEncrypt
{
    const AES_KEY = "qq3217834abcdefg"; //16位
    const AES_IV  = "1234567890123456"; //16位

    public static function aes_decrypt($str)
    {
        $decrypted = openssl_decrypt(base64_decode($str), 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV);

        return $decrypted;
    }

    public static function aes_encrypt($plain_text)
    {
        $encrypted_data = openssl_encrypt($plain_text, 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV);

        return base64_encode($encrypted_data);
    }
}




————————————————
原文作者:不忘初心
转自链接:https://learnku.com/articles/8584/php-and-web-end-symmetric-encryption-transmission-jsencryptcryptojs
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。

 

        $id = base64_encode(openssl_encrypt(base64_encode($id), 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV));
        $id = base64_decode(openssl_decrypt(base64_decode($id), 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV));
posted @ 2020-11-12 10:15  酷酷的城池  阅读(333)  评论(0编辑  收藏  举报