HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot//douyin.fehss.com/extend/com/Cryptdes.php
<?php
namespace com;

/**
 * 对称加密解密
 */
class Cryptdes
{
    // 加密方式
    const METHOD = 'AES-256-CBC';
    public $key;
    public $error = '';
    
    public $ivStatus;


    public function __construct($key, $ivStatus = true)
    {
        if (!function_exists('openssl_encrypt') || !function_exists('openssl_decrypt')) {
            $this->error = '未启用 openssl 扩展,请开启。';
        }
        $this->ivStatus = $ivStatus;
        $this->key = $key;
    }

    /**
     * 加密
     */
    public function encrypt($data)
    {
        $str_padded = json_encode($data);
        if (strlen($str_padded) % 16) {
            $str_padded = str_pad($str_padded, strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
        }
        if ($this->ivStatus) {
            $iv = $this->makeIv();
            $code = openssl_encrypt($str_padded, SELF::METHOD, $this->key, OPENSSL_NO_PADDING, $iv);
            return [
                'iv' => base64_encode($iv),
                'code' => base64_encode($code)
            ];
        } else {
            $code = openssl_encrypt($str_padded, SELF::METHOD, $this->key, OPENSSL_NO_PADDING);
            return base64_encode($code);
        }
    }

    /**
     * 解密
     */
    public function decrypt($code, $iv = '')
    {
        $code = base64_decode($code);
        $iv = base64_decode($iv);
        
        $res = openssl_decrypt($code, SELF::METHOD, $this->key, OPENSSL_NO_PADDING, $iv);
        return json_decode(trim($res), true);
    }

    /**
     * 生成 初始化向量 iv
     */
    public function makeIv()
    {
        $ivlen = openssl_cipher_iv_length(SELF::METHOD);
        return openssl_random_pseudo_bytes($ivlen);
    }
}