Home > Net >  Getting blank instead of decryption
Getting blank instead of decryption

Time:12-23

I trying to decrypt ID but getting nothing .I have used AES-256-CBC openssl encryption method it is being encrypte but not decrypted. Please help

Output Iam getting is here

encryptId===ZHJpTWNMN3o4SlFRU2JkTDZSeGJQQT09 decryptId===

Here is the class

class Cryptor
{

  private $encrypt_method = "AES-256-CBC";
//XDT-YUGHH-GYGF-YUTY-GHRGFR
  private $secret_key = "12345678901478523693216549875621";

  private $iv = "1234567890123456";

  function encryptId($id)
  {
    $options = 0;
    $key = hash('sha256', $this->secret_key);
    $iv = substr(hash('sha256', $this->iv), $options, 16);
    $id = openssl_encrypt($id, $this->encrypt_method, $key,$options, $iv);
    $id = base64_encode($id);
     return $id;
  }
  function decryptId($id)
  {
    $id = base64_decode($id);
    $options = 0;
    $key = hash('sha256', $this->secret_key);
    $iv = substr(hash('sha256', $this->iv), $options, 16);
    $id = openssl_decrypt($id, $this->encrypt_method, $key,$options, $iv);
    return $id;
  }
}
$cat_id = 1;
$crypt = new Cryptor();
echo '<br>';
echo 'encryptId==='.$crypt->encryptId($cat_id);
echo '<br>';
echo 'decryptId==='.$crypt->decryptId($cat_id);

CodePudding user response:

You need to decrypt the result of encrypting, not $cat_id.

$cat_id = 1;
$crypt = new Cryptor();
echo '<br>';
$encrypted = $crypt->encryptId($cat_id)
echo 'encryptId==='.$encrypted;
echo '<br>';
echo 'decryptId==='.$crypt->decryptId($encrypted);
  • Related