title: PHP GNUPG Decryption Error
body: <p>I am getting a decrypt failed response when I decrypt an encrypted string. Please see my two functions below. I am using PHP 5.9 and I have a public key, private key, and passphrase that are correct.</p>
  
  <pre><code>//-----------------------------------------------------------------------------
  //Function Name : Encrypt Data
  //Purpose : Check key into database and one the basis of key encrypt post data
  function encryptData($apiKey,$postBody){
      try {
          global $g_e_v,$gpg;
  
          $publicKeyResult = getKey($apiKey,'apikeys_public');
          $publicKey = $publicKeyResult-&gt;apikeys_public;
  
          //Set Enviroment variable for GNUPG.
          putenv("GNUPGHOME=".$g_e_v.".gnupg");
          $gpg-&gt;seterrormode(gnupg::ERROR_EXCEPTION);
          $info = $gpg-&gt;import($publicKey);
          var_dump($info);
          $gpg-&gt;addencryptkey($info['fingerprint']);
          $encryptData = $gpg-&gt;encrypt($postBody);
          return $encryptData;
      }
      catch (Exception $exception) {
  
                  echo $exception-&gt;getMessage();
      }
  }
  
  //-----------------------------------------------------------------------------
  //Function Name : Decrypt Data
  //Purpose : Check key into database and on the basis of key decrypt post data.
  function decryptData($apiKey,$postBody){
      try {
          global $g_e_v,$gpg;
  
          $privateKeyResult = getKey($apiKey,'apikeys_private,apikeys_passphrase');
          $privateKey = $privateKeyResult-&gt;apikeys_private;
          $phrase = $privateKeyResult-&gt;apikeys_passphrase;
  
          //Set Enviroment variable for GNUPG.
          putenv("GNUPGHOME=".$g_e_v.".gnupg");
          $gpg-&gt;seterrormode(gnupg::ERROR_EXCEPTION);
          $info = $gpg-&gt;import($privateKey);
          var_dump($info);
          $gpg-&gt;adddecryptkey($info['fingerprint'],$phrase);   
          $decryptData = $gpg-&gt;decrypt($postBody);
          return $decryptData;
      }
      catch (Exception $exception) {
                  echo $exception-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p>I am calling it like this:</p>
  
  <pre><code>$en = encryptData('SGFmQMPtkFlWYM','This is a test');
  
  echo $en;
  $postBody = decryptData('SGFmQMPtkFlWYM', $en);
  </code></pre>
  
  <p>And my output is:</p>
  
  <pre><code>/home/ubuntu/workspace/API/GlobalFunctions.php:81:
  array (size=9)
    'imported' =&gt; int 0
    'unchanged' =&gt; int 1
    'newuserids' =&gt; int 0
    'newsubkeys' =&gt; int 0
    'secretimported' =&gt; int 0
    'secretunchanged' =&gt; int 0
    'newsignatures' =&gt; int 0
    'skippedkeys' =&gt; int 0
    'fingerprint' =&gt; string '51103C105AC9E46F06880CA6A1511AEACC486E40' (length=40)
  -----BEGIN PGP MESSAGE----- Version: GnuPG v2.0.22 (GNU/Linux) hQIMA3Sy9vCeHG7RARAAwGXu4fnERB44ZXIITbx/MaW/xKHmJa9kEh4ep76Z3TKT TklqYIUbyvvpUrPsAXUUMHAlAiMCiLHbL1aySexSzQ2DhPHjjxEhW3gSVqjNwUxw EclaMHVsTK16EpePMJogWWfY9i1S+VlSQLaaopyiF85b3id94Ik3Pb8FyZ5QLmia Iw5rHs+lc89snZkn0HQo4G+nW0w5E7O1VZR/ktN/IUgdkKB95DPtS88Dv4CAGisK 21+Tfu3q/a7FVJj8FD0aTc7jM19cp1CaWyPk5k0elm3DEEG2qNw7bA4iy6PjPos1 mRahQtHZFr0WtpqPV52Nqimcha3CDjdzJpI0KP94sOXK7dDG2rRAHbcXgYv+Bei8 z95/OK76InPeJQY7sbf7UEyAqh4baAnTgQkXLfkHdKOlrPGw/N/y7IC2Yf2laMQY Q61mfPQ8ri3fxOaBisP8vaBGQAaqvCgje220WzTc3h+D/wYShPr28ftj68gwC4XD N++r2q6TEtbMhJNziq1RrN6m6tR060UBRjv/STCh1tNPFVecrFo5O8F5H78ag37H lI9Ngt7HSCtUF1W5gyFbRbXn3NVm6cBDmPS8Qmi7mDmmta7fbqtzj/R088BKfekD Gl49KZeNUfbPdeMxwU5V23cvCRzUIhXyxs31j1l5qdqH7HYdvtIOPkEfoJfx+NDS QQGum5038urfPruTf1cIj12sO2MoJFH4dlgPtGgi7CSN1/omtWjXJ8gKVwjVT0B5 F2oztlKKkVUrd551dchqkmnh =2kJj -----END PGP MESSAGE-----
  /home/ubuntu/workspace/API/GlobalFunctions.php:108:
  array (size=9)
    'imported' =&gt; int 0
    'unchanged' =&gt; int 1
    'newuserids' =&gt; int 0
    'newsubkeys' =&gt; int 0
    'secretimported' =&gt; int 0
    'secretunchanged' =&gt; int 1
    'newsignatures' =&gt; int 0
    'skippedkeys' =&gt; int 0
    'fingerprint' =&gt; string '51103C105AC9E46F06880CA6A1511AEACC486E40' (length=40)
  decrypt failed
  /home/ubuntu/workspace/API/Test.php:9:null
  </code></pre>