2048, "private_key_type" => OPENSSL_KEYTYPE_RSA ]); openssl_pkey_export($res, $privKey); $pubKey = openssl_pkey_get_details($res); return [ 'privatekey' => $privKey, 'publickey' => $pubKey["key"] ]; } # 以下是数字加密解密 const PASSWORD = 'MAW512P89WIAUW9G7OHQWVGAPX51WPSN'; /** * 数字加密函数 * * @param int $number * @param string $password * @return string */ public static function encryptNumber(int $number, string $password = '') { // 生成随机的初始向量 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $password = $password ?: self::PASSWORD; // 加密数字 $encrypted = openssl_encrypt($number, 'aes-256-cbc', $password, 0, $iv); // 将初始向量和加密后的数据进行编码并拼接起来 $encoded = base64_encode($iv . $encrypted); return $encoded; } /** * 数字解密函数 * * @param string $encrypted * @param string $password * @return int */ public static function decryptNumber(string $encrypted, string $password = '') { // 解码加密后的数据 $decoded = base64_decode($encrypted); // 提取初始向量和加密后的数据 $ivLength = openssl_cipher_iv_length('aes-256-cbc'); $iv = substr($decoded, 0, $ivLength); $encryptedData = substr($decoded, $ivLength); $password = $password ?: self::PASSWORD; // 解密数据 $decrypted = openssl_decrypt($encryptedData, 'aes-256-cbc', $password, 0, $iv); return $decrypted; } }