首页 > 解决方案 > openssl_decrypt 是否在 Ubuntu 上运行的 PHP 上使用任何网络调用?

问题描述

我正在使用 PHP 函数 openssl_encrypt 来加密聊天消息。当我可以访问 Internet 时它工作正常,但是当我在未连接到 Internet 的 Ubuntu 服务器上运行相同的代码时,它很慢。

谁能指出我解决此问题的正确方向?openssl_decrypt 是否在 Ubuntu 上运行的 PHP 上使用任何网络调用?

加密消息:

$inp_text = "SOME TEXT THAT WILL BE ENCRYPTED";
$get_current_channel_encryption_key = "SOME ENCRYPTION KEY";

$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($inp_text, $cipher, $get_current_channel_encryption_key, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $get_current_channel_encryption_key, $as_binary=true);
$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );

解密消息:

$get_message_text = "SOME ENCRYPTED MESSAGE";
$get_current_channel_encryption_key = "SOME ENCRYPTION KEY";

$c = base64_decode($get_message_text);
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $get_current_channel_encryption_key, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $get_current_channel_encryption_key, $as_binary=true);
if (hash_equals($hmac, $calcmac))//PHP 5.6+ timing attack safe comparison
{
    $get_message_text = "$original_plaintext";
}

标签: phpubuntu

解决方案


推荐阅读