title: Trying to decrypt with aes-256-gcm with php
body: <p>I wondered whether anyone can help,</p>
  
  <p>I am using encryption method aes-256-gcm, I can encrypt, but cannot decrypt.</p>
  
  <p>Below is my code, can anyone see where I'm going wrong</p>
  
  <pre><code>$textToDecrypt = $_POST['message'];
  $password = '3sc3RLrpd17';
  $method = 'aes-256-gcm'; 
  $tag_length = 16;
  $password = substr(hash('sha256', $password, true), 0, 32);
  $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
  $decrypted = openssl_decrypt(base64_decode($textToDecrypt), $method, 
  $password, OPENSSL_RAW_DATA, $iv, $tag_length);
  </code></pre>
  
  <p>Encryption code</p>
  
  <pre><code>$textToEncrypt = $_POST['message'];
  $password = '3sc3RLrpd17';
  $method = 'aes-256-gcm'; 
  $tag_length = 16;
  
  
  $password = substr(hash('sha256', $password, true), 0, 32);
  
  
  
  $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . 
  chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . 
  chr(0x0) . chr(0x0) . chr(0x0);
  
  
  $encrypted = base64_encode(openssl_encrypt($textToEncrypt, $method, 
  $password, OPENSSL_RAW_DATA, $iv, $tag_length));
  </code></pre>