Solved

Trouble with decrypting PGP encrypted file using PHP & GnuPG

Posted on 2014-04-30
4
387 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
  • 2
4 Comments
 
LVL 108

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 108

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now