2017-12-10 12 views
0

は、PHPのOpenSSL_encrypt/OpenSSL_decryptを使用してテキストを暗号化/復号化しようとしているが、IAMは、ここでそれを を行う上でいくつかの問題を取得しようとして暗号化/復号化するためにTyringは私がしようとしたものです:IAM PHPのOpenSSL_encrypt/OpenSSL_decryptを使用してデータ

私のコードは

const OPENSSL_ENCRYPTz = 0; 
const OPENSSL_DECRYPTz = 1; 
function OpenSSLEndeCrypt($action = 0, $string = '') 
{ 
    $output = false; 
    $encrypt_method = "AES-256-CBC"; 
    //$secret_key = 'This is my secret key'; 
    // $secret_iv = 'This is my secret iv'; 
    $key = openssl_random_pseudo_bytes(32); 
    //$key = hash('sha256', $secret_key); 
    $ivlen = openssl_cipher_iv_length($encrypt_method); 
    $iv = openssl_random_pseudo_bytes($ivlen); 
    //$iv = substr(hash('sha256', $secret_iv), 0, 16); 
    if ($action == $OPENSSL_ENCRYPTz) 
    { 
     $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); 
     $output = base64_encode($output); 
    } 
    else if($action == $OPENSSL_DECRYPTz) 
    { 
     $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); 
    } 
    return $output; 
} 
$encrypted_text = OpenSSLEndeCrypt($OPENSSL_ENCRYPTz, 'cs2xCp2F6bk'); 
echo 'Your Encrypted Text: '. $encrypted_text. '<br />'; 
echo 'Your Decrypted Text: '. OpenSSLEndeCrypt($OPENSSL_DECRYPTz, $encrypted_text). '<br />'; 


ERROR/ERRORS/NOTICES (testing in XAMPP PHP 5.6):- 
    Notice: Undefined variable: OPENSSL_ENCRYPTz in \tests.php on line 182 
    Notice: Undefined variable: OPENSSL_ENCRYPTz in tests.php on line 171 
    Your Encrypted Text: NUdXSWFOVms5UHhHMFZrWGp4dE92QT09 
    Notice: Undefined variable: OPENSSL_DECRYPTz in tests.php on line 184 
    Notice: Undefined variable: OPENSSL_ENCRYPTz in tests.php on line 171 
    Your Decrypted Text: bTVjS2FWeFhkSWVPbG9Xd3BrYnp4ZytWOTdDZmxITXMwZjVsNzZvbExoU25XcEExVmVHaVhZRkt5TE5jTFZ0Mg== 

答えて

1

定数は変数名がありません$接尾辞を必要としません。だから、単にあなたの定数から$を削除 すなわち

if ($action == $OPENSSL_ENCRYPTz) 
     // ^The error 

は、あなたがこれらの定数名を使用どこでもこれは修正必要

if ($action == OPENSSL_ENCRYPTz) 

でなければなりません。

関連する問題