Solved

Trouble with decrypting PGP encrypted file using PHP & GnuPG

Posted on 2014-04-30
4
601 Views
Last Modified: 2016-05-21
We have the keys setup and GnuPG setup on the server, we are able to decrypt the file using the command line without any errors. However, when we use the GnuPG class for PHP we run into trouble. Here is the code we currently have, although we have gone through many variations and tried many different things, any advice is welcome as this is the first time we have worked with PGP let alone GnuPG.

$encryptedContent = file_get_contents($encryptedFileName);
		 
		$passphrase = '************';
		$gpg = new gnuPG('gpg', '/var/www/html/.gnupg');		
		$secretKeys = $gpg->ListKeys('secret');
		
		if (is_array($secretKeys))
		{
			try
			{
				$decryptedContent = $gpg->decrypt($secretKeys[0]['KeyID'], $passphrase, $encryptedContent);
			}
			catch (Exception $e)
			{
				echo "PGP Error: " . $gpg->error . "<br/>";
				echo "Exception: " . $e;
			}
			echo "PGP Error: " . $gpg->error . "<br/>";
			echo "Decrypted Content: " . $decryptedContent;
		}

Open in new window

0
Comment
Question by:bkort1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40036849
Got a Neglected Question Alert on this one.

Given the rather thin collection of user contributed notes, I would surmise that this is not one of the mainstream parts of PHP.  If you need a simple encryption that works, this little script will demonstrate how I've done it successfully.

<?php // demo/encrypt_decrypt.php
error_reporting(E_ALL);

// MAN PAGE: http://php.net/manual/en/ref.mcrypt.php

class Encryption
{
    protected $key;

    public function __construct($key='quay')
    {
        // THE KEY MUST BE KNOWN TO BOTH PARTS OF THE ALGORITHM
        $this->key = $key;
    }

    public function encrypt($text)
    {
        // ENCRYPT THE DATA
        $data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB);

        // MAKE IT base64() STRING SAFE FOR STORAGE AND TRANSMISSION
        return base64_encode($data);
    }

    public function decrypt($text)
    {
        // DECODE THE DATA INTO THE BINARY ENCRYPTED STRING
        $text = base64_decode($text);

        // DECRYPT THE STRING
        $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB);

        // DECLOP NUL-BYTES BEFORE THE RETURN
        return trim($data);
    }
}

// INSTANTIATE AN ENCRYPTION OBJECT FROM THE CLASS
$c = new Encryption();

// INITIALIZE VARS FOR LATER USE IN THE HTML FORM
$encoded = $decoded = NULL;

// IF ANYTHING WAS POSTED SHOW THE DATA
if (!empty($_POST["clearstring"]))
{
    $encoded = $c->encrypt($_POST["clearstring"]);
    echo "<br/>{$_POST["clearstring"]} YIELDS ENCODED ";
    var_dump($encoded);
}

if (!empty($_POST["cryptstring"]))
{
    $decoded = $c->decrypt($_POST["cryptstring"]);
    echo "<br/>{$_POST["cryptstring"]} YIELDS DECODED ";
    var_dump($decoded);
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<FORM
<form method="post">
<input name="clearstring" value="$decoded" />
<input type="submit" value="ENCRYPT" />
<br/>
<input name="cryptstring" value="$encoded" />
<input type="submit" value="DECRYPT" />
</form>
FORM;

echo $form;

Open in new window

If I have some time today (and my PHP installation has all the bells and whistles) I will try to show something using GnuPG.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40036866
Update: The extension is not installed on my server.  Sorry, but it looks like this is (at least) a research project.
http://php.net/manual/en/gnupg.installation.php

From https://www.gnupg.org/related_software/gpgme/ this is unfortunate:
See download section to download the latest tarball.


Best of luck.  Sorry I couldn't help any further.
0
 

Accepted Solution

by:
bkort1 earned 0 total points
ID: 40086720
It turned out to be an old/invalid DLL in PHP.  Once updated, it decrypted properly.
0

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question