[英]Tyring to encrypt/decrypt data using PHP's OpenSSL_encrypt/OpenSSL_decrypt
iam trying to trying to encrypt/decrypt an text using PHP's OpenSSL_encrypt/OpenSSL_decrypt but iam getting some problems on doing it here is what i have tried to do: 我试图使用PHP的OpenSSL_encrypt / OpenSSL_decrypt来加密/解密文本,但是我在这里遇到一些问题,这是我尝试做的事情:
my 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==
Constants do not require the $
suffix that a variable name does. 常量不需要像变量名那样的
$
后缀。 So simply remove the $
from your constants ie 所以只需从常量中删除
$
即
if ($action == $OPENSSL_ENCRYPTz)
// ^ The error
should be 应该
if ($action == OPENSSL_ENCRYPTz)
This needs amending wherever you use those constants names. 无论您在何处使用这些常量名称,都需要进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.