简体   繁体   中英

PGP Encryption - PHP openssl

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM