This post was updated 719 days ago and some of the ideas may be out of date.

前端代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script>
    function rc4(content, key, operation) {
        const iv = CryptoJS.enc.Utf8.parse(key.substring(0, 16));
        key = CryptoJS.enc.Utf8.parse(CryptoJS.MD5(key).toString());
        if (operation) {
            return CryptoJS.RC4.decrypt(content, key, {
                iv: iv,
                padding: CryptoJS.pad.Pkcs7
            }).toString(CryptoJS.enc.Utf8)
        }
        return CryptoJS.RC4.encrypt(content, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }).toString()
    }

    console.log(rc4('hello world', '1234567890', false));

</script>
</body>
</html>

后端代码:

<?php

/**
 * Created by PhpStorm.
 * User: LinFei
 * Created time 2023/01/17 17:41:47
 * E-mail: fly@eyabc.cn
 */

function rc4(string $content, string $key, $operation = false)
{
    $key = md5($key);
    if ($operation) {
        return openssl_decrypt(base64_decode($content), 'rc4', $key, OPENSSL_RAW_DATA);
    }
    return base64_encode(openssl_encrypt($content, 'rc4', $key, OPENSSL_RAW_DATA));
}


var_dump(rc4('LJi8Ufyb3iu4PjQ=', '1234567890', true));