-1

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?

Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
Jay2001
  • 59
  • 7
  • It looks like the output is just base64 encoded, is there a way to tell gnupg to not base64 encode after it encrypts? – Jay2001 Nov 15 '20 at 12:51

1 Answers1

1

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;
?>
Michael Fehr
  • 5,827
  • 2
  • 19
  • 40
  • Perfect solution, thanks. Just for completeness for any future readers if you are using the "$gpg = new gnupg()" style the answer would be "$gpg -> setarmor(0);" – Jay2001 Nov 17 '20 at 09:50