php MagicCrypt 加密解密 与 rust MagicCrypt 通用

180it 2023-05-04 AM 2494℃ 0条

PHP用到mcrypt模块来实作AES,因此需要激活mcrypt模块

在线MagicCrypt加密解密 http://www.txttool.com/t/?id=NjA2

Snipaste_2023-05-04_10-38-11.png

<?php

namespace org\magiclen\magiccrypt;

class MagicCrypt {
    private $iv;
    private $key;
    private $bit;
    public
    function __construct($key = '', $bit = 128, $iv = '') {
        switch ($bit) {
            case 64:
                {
                    $this -> key = MagicCrypt::crc64($key);
                    if ($iv != '') {
                        $this -> iv = MagicCrypt::crc64($iv);
                    } else {
                        $this -> iv = chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0);
                    }
                }
                break;
            case 128:
            case 192:
            case 256:
                {
                    switch ($bit) {
                        case 128:
                            $this -> key = hash('MD5', $key, true);
                            break;
                        case 192:
                            $this -> key = hash('tiger192,3', $key, true);
                            break;
                        case 256:
                            $this -> key = hash('SHA256', $key, true);
                            break;
                    }
                    if ($iv != '') {
                        $this -> iv = hash('MD5', $iv, true);
                    } else {
                        $this -> iv = chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0);
                    }
                }
                break;
            default:
                throw new\ Exception('The key must be 8 bytes(64 bits), 16 bytes(128 bits), 24 bytes(192 bits) or 32 bytes(256 bits)!');
        }
        $this -> bit = $bit;
    }
    public
    function encrypt($str) {
        $algorithm = $this -> bit > 64 ? MCRYPT_RIJNDAEL_128 : 'des';
        $module = @mcrypt_module_open($algorithm, '', MCRYPT_MODE_CBC, '');@
        mcrypt_generic_init($module, $this -> key, $this -> iv);
        $block = @mcrypt_get_block_size($algorithm, MCRYPT_MODE_CBC);
        $pad = $block - (strlen($str) % $block);
        $str.= str_repeat(chr($pad), $pad);
        $encrypted = @mcrypt_generic($module, $str);@
        mcrypt_generic_deinit($module);@
        mcrypt_module_close($module);
        return base64_encode($encrypted);
    }
    public
    function decrypt($str) {
        $algorithm = $this -> bit > 64 ? MCRYPT_RIJNDAEL_128 : 'des';
        $module = @mcrypt_module_open($algorithm, '', MCRYPT_MODE_CBC, '');@
        mcrypt_generic_init($module, $this -> key, $this -> iv);
        $str = @mdecrypt_generic($module, base64_decode($str));@
        mcrypt_generic_deinit($module);@
        mcrypt_module_close($module);
        $slast = ord(substr($str, -1));
        $str = substr($str, 0, strlen($str) - $slast);
        return $str;
    }
    private static
    function crc64Table() {
        $crc64tab = [];
        $poly64rev = (0x42F0E1EB << 32) | 0xA9EA3693;
        $mask1 = 1 << 63;
        $mask2 = 1;
        for ($i = 1; $i < 64; ++$i) {
            $mask2 = ($mask2 << 1) + 1;
        }
        for ($i = 0; $i < 256; ++$i) {
            $v = $i;
            for ($j = 0; $j < 64; ++$j) {
                if (($v & $mask1) == 0) {
                    $v = $v << 1;
                } else {
                    $v = $v << 1;
                    $v = $v ^ $poly64rev;
                }
            }
            $crc64tab[$i] = $v & $mask2;
        }
        return $crc64tab;
    }
    private static
    function crc64($string) {
        static $crc64tab;
        if ($crc64tab === null) {
            $crc64tab = MagicCrypt::crc64Table();
        }
        $h8Mask = ~(0xff << 56);
        $crc = ~0;
        $length = strlen($string);
        for ($i = 0; $i < $length; ++$i) {
            $lookupidx = ((($crc >> 56) & $h8Mask) ^ ord($string[$i])) & 0xff;
            $crc = ($crc << 8) ^ $crc64tab[$lookupidx];
        }
        $crc = $crc ^ ~0;
        return pack('CCCCCCCC', ($crc >> 56) & $h8Mask, (($crc << 8) >> 56) & $h8Mask, (($crc << 16) >> 56) & $h8Mask, (($crc << 24) >> 56) & $h8Mask, (($crc << 32) >> 56) & $h8Mask, (($crc << 40) >> 56) & $h8Mask, (($crc << 48) >> 56) & $h8Mask, (($crc << 56) >> 56) & $h8Mask);
    }
}

?>

<?php

    $mc = new MagicCrypt('xxxx', 256);
    use org\magiclen\magiccrypt\MagicCrypt;

    echo $mc->encrypt('U+B81NZlIloELvF/ErNswGE7OBomUI5hl+DajB2lnP9XaSKpNW6D3ryaJ58Gr4tN'), "<br/>";
    echo $mc->decrypt($mc->encrypt('U+B81NZlIloELvF/ErNswGE7OBomUI5hl+DajB2lnP9XaSKpNW6D3ryaJ58Gr4tN'));

?>

来源:https://github.com/magiclen/MagicCrypt

rust 库 magic-crypt = "3.1.12"

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php MagicCrypt 加密解密 与 rust MagicCrypt 通用