与PHP解密相匹配的Javascript加密

Javascript encryption that match with PHP decryption

本文关键字:Javascript 加密 PHP 解密      更新时间:2023-09-26

我有下面的php代码,我想创建一个与下面php代码相同的Javascript函数。此外,在Javascript中加密的数据也可以在php中解密。`

<?php
class Security {
    public static function encrypt($input, $key) {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); 
        $input = Security::pkcs5_pad($input, $size); 
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
        mcrypt_generic_init($td, $key, $iv); 
        $data = mcrypt_generic($td, $input); 
        mcrypt_generic_deinit($td); 
        mcrypt_module_close($td); 
        $data = base64_encode($data); 
        return $data; 
    } 
    private static function pkcs5_pad ($text, $blocksize) { 
        $pad = $blocksize - (strlen($text) % $blocksize); 
        return $text . str_repeat(chr($pad), $pad); 
    } 
    public static function decrypt($sStr, $sKey) {
        $decrypted= mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128,
            $sKey, 
            base64_decode($sStr), 
            MCRYPT_MODE_ECB
        );
        $dec_s = strlen($decrypted); 
        $padding = ord($decrypted[$dec_s-1]); 
        $decrypted = substr($decrypted, 0, -$padding);
        return $decrypted;
    }   
}
?>

`

在javascript端使用crypto.js。此处的参考

Javascript

  var key = CryptoJS.enc.Hex.parse("0123456789abcdef0123456789abcdef");
    var iv = CryptoJS.enc.Hex.parse("abcdef9876543210abcdef9876543210");
    /*
     if you wish to have a more friendly key, you can convert letters to Hex this way:
     var a = "D";
     var hex_D = a.charCodeAt(0).toString(16);
     just to mention,
     if it were to binary, it would be:
     var binary_D = a.charCodeAt(0).toString(2);
     */
    var secret = "secret key goes here";
    //crypted
    var encrypted = CryptoJS.AES.encrypt(secret, key, {iv : iv});
    //and the ciphertext put to base64
    encrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    //Assuming you have control on the server side, and know the key and iv hexes(we do),
    //the encrypted var is all you need to pass through ajax,
    //Let's follow with welcomed pure JS style, to reinforce one and other concept if needed
    var xh = new XMLHttpRequest();
    xh.open("POST", "php.php", true);
    xh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xh.send("encrypted=" + encodeURIComponent(encrypted));
    xh.onreadystatechange = function() {
        if (xh.readyState == 4 && xh.status == 200) {
            var myArr = (xh.responseText);
            console.log(myArr);
        }
    };

在PHP

<?php
//Here we have the key and iv which we know, because we have just chosen them on the JS,
//the pack acts just like the parse Hex from JS
$key = pack("H*", "0123456789abcdef0123456789abcdef");
$iv = pack("H*", "abcdef9876543210abcdef9876543210");
//Now we receive the encrypted from the post, we should decode it from base64,
$encrypted = base64_decode($_POST["encrypted"]);
$shown = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
echo trim($shown);
//Although the decrypted is shown, there may be needed to trim and str_replace some 'r 'n 'x06 'x05, if there is not a better "trim" way to do it though
?>

您也可以使用JXG压缩机