doulian4762
2017-12-10 17:45
浏览 352
已采纳

尝试使用PHP 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:

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

图片转代码服务由CSDN问答提供 功能建议

iam尝试使用PHP的OpenSSL_encrypt / OpenSSL_decrypt尝试加密/解密文本但是我遇到了一些问题 我试图做的事情是:

我的代码

  const OPENSSL_ENCRYPTz = 0; 
 nbspst OPENSSL_DECRYPTz = 1; 
function OpenSSLEndeCrypt  ($ action = 0,$ string ='')
 {
 $ output = false; 
 $ encrypt_method =“AES-256-CBC”; 
 // $ secret_key ='这是我的密钥';  
 // $ secret_iv ='这是我的秘密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)\  n {
 $ output = openssl_encrypt($ string,$ encrypt_method,$ key,0,$ iv); 
 $ output = base64_encode($ output); 
} 
 else if($ action == $ OPENSSL_DECRYPTz)  
 {
 $ output = openssl_decrypt(bas  e64_decode($ string),$ encrypt_method,$ key,0,$ iv); 
} 
返回$ output; 
} 
 $ encrypted_text = OpenSSLEndeCrypt($ OPENSSL_ENCRYPTz,'cs2xCp2F6bk'); 
echo'您的加密 文字:'。  $ encrypted_text。  '&lt; br /&gt;'; 
echo'你的解密文字:'。  OpenSSLEndeCrypt($ OPENSSL_DECRYPTz,$ encrypted_text)。  '&lt; br /&gt;'; 
 
 
ERROR / ERRORS / NOTICES(在XAMPP PHP 5.6中测试): -  
注意:未定义的变量:第182行的\ tests.php中的OPENSSL_ENCRYPTz 
注意:未定义的变量 :第171行的tests.php中的OPENSSL_ENCRYPTz 
您的加密文本:NUdXSWFOVms5UHhHMFZrWGp4dE92QT09 
注意:未定义的变量:第184行的tests.php中的OPENSSL_DECRYPTz 
注意:未定义的变量:第171行的tests.php中的OPENSSL_ENCRYPTz 
您的解密 文字:bTVjS2FWeFhkSWVPbG9Xd3BrYnp4ZytWOTdDZmxITXMwZjVsNzZvbExoU25XcEExVmVHaVhZRkt5TE5jTFZ0Mg == 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dslfq06464 2017-12-10 19:04
    已采纳

    Constants do not require the $ suffix that a variable name does. So simply remove the $ from your constants i.e.

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

    should be

    if ($action == OPENSSL_ENCRYPTz) 
    

    This needs amending wherever you use those constants names.

    点赞 打赏 评论

相关推荐 更多相似问题