I have a public key with extension .asc that I need to use to encrypt data and to send it in a plain text file. I am trying to accomplish this in a windows machine using php. Any ideas? Thanks,
Asked
Active
Viewed 1,848 times
0
-
1In PECL, there is a gnupg extension http://www.php.net/manual/en/book.gnupg.php – gen_Eric Jan 21 '14 at 18:36
-
rocket-hazmat: I don't have any problem configuring it in linux. Windows is what being a pain... I was hoping to find the dll I can refer to within the INI file but I don't see any out there – Voltaire Jan 21 '14 at 19:50
2 Answers
1
Make sure you installed the gnupg extension for PHP.
$gpg = new gnupg();
$publicData = file_get_contents('public.asc');
$publicKey = $gpg->import($publicData);
$gpg->addencryptkey($publicKey['fingerprint']);
echo $gpg->encrypt('Data to encrypt');
Instead of encrypting a constant, replace by the file contents, and store the encrypted message wherever you want.

Jens Erat
- 37,523
- 16
- 80
- 96
-
gnupg extension isn't that straight forward in windows. I tried installing it using PECL but getting an error "ERROR: The DSP gnupg.dsp does not exist." I may have to compile it from the source ... – Voltaire Jan 21 '14 at 19:56
-
You might be happier just calling the binary. Make sure to put the public key into the web servers GnuPG directory, and just call `gpg.exe -r DEADBEEF -d file-to-encrypt >encrypted-file.asc`. You might have to use GnuPG's full path. Or simply use a Linux box as web server... – Jens Erat Jan 21 '14 at 23:10
-
0
first create new folder owner by www-data
mkdir /var/www/.gnupg
sudo chown -R www-data:www-data /var/www/.gnupg
second try follow php code:
putenv("GNUPGHOME=/var/www/.gnupg");
$gpg = new gnupg();
$publicData = file_get_contents('/var/www/html/web/resources/keys/public.asc');
$privateData = file_get_contents('/var/www/html/web/resources/keys/SECRET.asc');
$publicKey = $gpg->import($publicData);
$gpg->addencryptkey($publicKey['fingerprint']);
$encrypt = ($gpg->encrypt('Data to encrypt'));
echo $encrypt;

Đọc truyện hay
- 1,913
- 21
- 17