stevennathaniel

PHP AES 256 CBC : Decrypt Plain Text

Jan 3rd, 2021
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php  
  2.  
  3.     $key = "74b8d8e7f552f49a9f59642db34ce5601dcb726b52e0";
  4.  
  5.     $ciphertext = "OeNa+bCeII57X0hSf0IBZLSYed5SUGSEOZMocYDzyTNlO0MRervabo/CbIbHsv3vpVaLhFHBd9j06MuR9VMQmmNp8N9h4f8HIC5m768RbDXi1O05Yxky7gl4EPTMYphnsBu3flqUQwzjJd3YBJJf/Q==";
  6.  
  7.     $c = base64_decode($ciphertext);
  8.  
  9.     $ivlen = openssl_cipher_iv_length($cipher="AES-256-CBC");
  10.  
  11.     $iv = substr($c, 0, $ivlen);
  12.  
  13.     $hmac = substr($c, $ivlen, $sha2len=32);
  14.  
  15.     $ciphertext_raw = substr($c, $ivlen+$sha2len);
  16.  
  17.     $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
  18.  
  19.     $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
  20.  
  21.     if (hash_equals($hmac, $calcmac)){
  22.  
  23.         echo $original_plaintext."\n";
  24.     }
  25.  
  26.  
  27. ?>
RAW Paste Data