Solved

Trouble with decrypting PGP encrypted file using PHP & GnuPG

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

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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

828 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