Почему я не могу расшифровать зашифрованный mcrypt текст с помощью opnssl, используя blowfisch и PHP 7.x - PullRequest
1 голос
/ 01 апреля 2020

Почему я не могу расшифровать зашифрованный mcrypt текст с помощью openssl

Я зашифровал информацию в базе данных и в приложениях на мобильных устройствах.

До сих пор они были зашифрованы и расшифрованы. на сервере с PHP 7.0 и mcrypt. - Устаревшее сообщение там уже отображается.

В следующих PHP версиях mcrypt не будет. Поэтому я попытался сделать это с openssl. Хотя я также использую blowfi sh с режимом CFB в openssl, он не работает.

Что я делаю не так?

#
# mcrypt on Server with PHP 7.0

/**
 * encrypt with Blowfish and mcrypt
 */
function mcrypt_encrypt($plaintext, $key)
{
    $td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CFB, '');
    $ivsize = mcrypt_enc_get_iv_size($td);
    $iv = mcrypt_create_iv($ivsize, MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $crypttext = mcrypt_generic($td, $plaintext);
    mcrypt_generic_deinit($td);
    return $iv . $crypttext;
}

/**
 * decrypt with Blowfish and mcrypt
 */
function mcrypt_decrypt($crypttext, $key)
{
    $td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CFB, '');
    $ivsize = mcrypt_enc_get_iv_size($td);
    $iv = substr($crypttext, 0, $ivsize);
    $crypttext = substr($crypttext, $ivsize);
    mcrypt_generic_init($td, $key, $iv);
    $plaintext = mdecrypt_generic($td, $crypttext);
    mcrypt_generic_deinit($td);
    return $plaintext;
}

Это работает на PHP 7.0:

$plaintext = 'Hello World';
$mcrypt_crypttext = mcrypt_encrypt($plaintext,'secret');
$mcrypt_plaintext = mcrypt_decrypt($mcrypt_crypttext,'secret');

# $plaintext == $mcrypt_plaintext;

Новые функции с OpenSSL:

#
# openssl on Server with PHP 7.2

/**
 * encrypt with Blowfish and openssl
 */
function openssl_encrypt($plaintext, $key)
{
    $ivlen = openssl_cipher_iv_length('bf-cfb');
    $iv = openssl_random_pseudo_bytes($ivlen);
    $crypttext = openssl_encrypt($plaintext, 'bf-cfb', $key, OPENSSL_RAW_DATA, $iv);
    return $iv . $crypttext;
}

/**
 * decrypt with Blowfish and openssl
 */
function openssl_decrypt($crypttext, $key)
{
    $ivlen = openssl_cipher_iv_length('bf-cfb');
    $iv = substr($data, 0, $ivlen);
    $crypttext = substr($data, $ivlen);
    $plaintext = openssl_decrypt($crypttext, 'bf-cfb', $key, OPENSSL_RAW_DATA, $iv);
    return $plaintext;
}

Это также работает:

$openssl_crypttext = openssl_encrypt($plaintext,'secret');
$openssl_plaintext = openssl_decrypt($openssl_crypttext,'secret');

# $plaintext == $openssl_plaintext;

Но это не так - расшифровывает зашифрованный текст mcrypt:

$openssl_plaintext = openssl_decrypt($mcrypt_crypttext,'secret');

# $plaintext != $openssl_plaintext

Есть ли способ расшифровать зашифрованные данные с помощью PHP7 .2?

...