Skip to content
Advertisement

How do I encrypt a file with gnupg and php? [closed]

I am not a PGP expert and php isn’t my first language, so I am having some trouble using gnupg to encrypt a file. I can get it to encrypt strings just fine.

If I use PGPTool (windows) to encrypt my file and then open it in a text editor it looks like binary data…

…]/…D2Øþ3EaÚj'ƒ‚Ì
4aœ¶è[Šæ³7=q    [Ûµ|<j‘«™yÅʧÊ[¥å¾•§Ù³,–rnÃƦ⅖iüïÛµèCŠ

If I read in the file and encrypt it with gnupg i get a plain text result, as if it were to be pasted into an email.

-----BEGIN PGP MESSAGE-----
Version: GnuPG v2.0.22 (GNU/Linux)

hQMOAx1dL4VEMtgUEAv/cOuJDBZ8FIYk7kqsh2vOvW2WRUvOUi54xm1LPGxLPiMS

My problem is, I need it to be output as the first option for sending to a specific client. Is there a way to get gnupg and php to do this?

Advertisement

Answer

Refering to the PHP-manpages (https://www.php.net/manual/en/function.gnupg-setarmor.php) the default output is a text file with base64 encoded data:

-----BEGIN PGP MESSAGE-----
Version: GnuPG v2.0.22 (GNU/Linux)
hQMOAx1dL4VEMtgUEAv/cOuJDBZ8FIYk7kqsh2vOvW2WRUvOUi54xm1LPGxLPiMS
...

Using the gnupg-setarmor function you can set the output to a binary output:

gnupg_setarmor($res,0);

Here is the complete code:

<?php
// init
$res = gnupg_init();
// # add this line
gnupg_setarmor($res,0); // deactivate default armored output
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
$enc = gnupg_encrypt($res, "just a test");
echo $enc;
?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement