4

I have this RSA 2048 key on a local "key.key" file:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.11 (GNU/Linux)

...
...
...
-----END PGP PUBLIC KEY BLOCK-----

How can I use it in PHP to encrypt a string to send? I am on a shared hosting environment and I can't install GNUpg, so I figured the included OpenSSL functions are the way to go.

I tried this, but I get "...key parameter is not a valid public key..."

<?php

$text = 'hello jeremy how are you';
$key = openssl_pkey_get_public(chunkfile_get_contents('key.key')); 

openssl_public_encrypt($text, $encrypted, $key);
echo $crypted;
exit;
jstudios
  • 856
  • 1
  • 9
  • 26
  • OpenPGP and SSL have _nothing_ in common but similar use cases and both mostly using RSA. But that's it. There's a GnuPG module for PHP available, see http://stackoverflow.com/questions/15969740/encrypt-files-using-pgp-in-php/15971087#15971087 for details. On shared hosting, GnuPG will very likely be installed and available. Most shared hosting environments are ran by Linux, and pretty much all Linux distributions use GnuPG in their package manager. – Jens Erat Dec 30 '14 at 19:51
  • It's not on mine, I tried running it and I get the error that I always get when a certain function doesn't exist so I need an alternative. – jstudios Dec 30 '14 at 20:00

1 Answers1

4

OpenSSL does not support OpenPGP, formats and protocols are different. Together with PHP, the most reasonable way to go is interfacing GnuPG using PHP's GnuPG module, I provided an example in the question Encrypt files using PGP in PHP?. Accessing GnuPG directly might be possible if you can execute arbitrary applications from PHP, but this will very likely be restricted (and is considerably more dangerous regarding exploitability).

If you're asked to send OpenPGP encrypted messages but have no access to GnuPG and this module, find another hosting company providing GnuPG, ask your current provider to do so or setup your own web server.

Community
  • 1
  • 1
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Ended up using `phpseclib` since it's a downloadable library. My co-worker and I are working on making it work using other formats besides PGP. Thanks! – jstudios Dec 30 '14 at 20:58