Solved

Trouble with decrypting PGP encrypted file using PHP & GnuPG

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS ELB 5 74
Wordpress errors showing up, hacked? 4 33
servlet web applications   metadata-complete="true" or false 3 36
Dynamic varibles 5 32
Provide an easy one stop to quickly get the relevant information on common asked question on Ransomware in Expert Exchange.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now