2017-12-10 7 views
0

iam versuchen, auf es zu tun ist hier einen Text mit PHP OpenSSL_encrypt/OpenSSL_decrypt aber iam immer einige Probleme zum Verschlüsseln/Entschlüsseln zu versuchen, was ich zu tun haben versucht zu:Tyring zum Verschlüsseln/Entschlüsseln von Daten unter Verwendung von PHP OpenSSL_encrypt/OpenSSL_decrypt

meinen Code

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== 

Antwort

1

Konstanten erfordern nicht die $ Suffix, das ein Variablenname der Fall ist. So entfernen Sie einfach den $ von Ihren Konstanten heißt

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

if ($action == OPENSSL_ENCRYPTz) 

sein sollte, dies muss zur Änderung überall dort, wo Sie diese Konstanten Namen verwenden.

Verwandte Themen