Solved

Trouble with decrypting PGP encrypted file using PHP & GnuPG

Posted on 2014-04-30
4
571 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

732 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