Каковы проблемы с этим фрагментом PHP кодов проверки пароля? - PullRequest
0 голосов
/ 05 февраля 2020

Я пытаюсь написать веб-страницу, и я реализовал две функции для шифрования и дешифрования строки с помощью функций openssl.

Функция шифрования с использованием openssl:
checkInputs () является самореализуемой функцией и возвращает htmlspecialchars ()

function encode($data) {
$data = checkInputs($data);

# Crypting with openssl method
// Store the cipher method
// Store cipher method 
$ciphering = "BF-CBC";

// Use OpenSSl encryption method 
$iv_length = openssl_cipher_iv_length($ciphering); 
$options = 0;

// Use random_bytes() function which gives 
// randomly 16 digit values 
$encryption_iv = random_bytes($iv_length);

// Alternatively, we can use any 16 digit 
// characters or numeric for iv 
$encryption_key = openssl_digest(php_uname(), 'MD5', TRUE);

// Encryption of string process starts 
$encryption = openssl_encrypt($data, $ciphering, $encryption_key, $options, $encryption_iv);
return $encryption;
}

Функция дешифрования с использованием openssl:

function decode($data) {
#codes to decode data
$ciphering = "BF-CBC";
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;
$decryption_iv = random_bytes($iv_length);
$decryption_key = openssl_digest(php_uname(), 'MD5', TRUE);
$decryption = openssl_decrypt ($data, $ciphering, $decryption_key, $options, $decryption_iv);
return $decryption;
}

Проблема в том, что кодирование ($ string) не совпадает с декодированием ($ string).

$string = "Welcome";
if( encode($string) == decode($string) ) {
...
} // returns FALSE, and the condition isn't true as well.

1 Ответ

0 голосов
/ 05 февраля 2020

Вы кодируете строку и сравниваете ее с декодированием той же строки:

$string = "Welcome";
$encode = encode($string);
if( $string == decode($encode) ) {
...
}

Или даже:

$string = "Welcome";
if( $string == decode(encode($encode)) ) {
...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...