13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Encryption in <strong>PHP</strong><br />

423<br />

<strong>and</strong> create a file named test.txt.asc. If you open test.txt.asc, you should see an<br />

encrypted message like this:<br />

-----BEGIN PGP MESSAGE-----<br />

Version: GnuPG v1.0.3 (GNU/Linux)<br />

Comment: For info see http://www.gnupg.org<br />

hQEOA0DU7hVGgdtnEAQAhr4HgR7xpIBsK9CiELQw85+k1QdQ+p/FzqL8tICrQ+B3<br />

0GJTEehPUDErwqUw/uQLTds0r1oPSrIAZ7c6GVkh0YEVBj2MskT81IIBvdo95OyH<br />

K9PUCvg/rLxJ1kxe4Vp8QFET5E3FdII/ly8VP5gSTE7gAgm0SbFf3S91PqwMyTkD<br />

/2oJEvL6e3cP384s0i8lrBbDbOUAAhCjjXt2DX/uX9q6P18QW56UICUOn4DPaW1G<br />

/gnNZCkcVDgLcKfBjbkB/TCWWhpA7o7kX4CIcIh7KlIMHY4RKdnCWQf271oE+8i9<br />

cJRSCMsFIoI6MMNRCQHY6p9bfxL2uE39IRJrQbe6xoEe0nkB0uTYxiL0TG+FrNrE<br />

tvBVMS0nsHu7HJey+oY4Z833pk5+MeVwYumJwlvHjdZxZmV6wz46GO2XGT17b28V<br />

wSBnWOoBHSZsPvkQXHTOq65EixP8y+YJvBN3z4pzdH0Xa+NpqbH7q3+xXmd30hDR<br />

+u7t6MxTLDbgC+NR<br />

=gfQu<br />

-----END PGP MESSAGE-----<br />

You should be able to transfer this file to the system where you generated the key initially<br />

<strong>and</strong> run<br />

gpg test.txt.asc<br />

to retrieve your original text.The text will be written to a file with the same name as it<br />

had before—in this case, test.txt.<br />

To have the text echoed to the screen, use the -d flag:<br />

gpg -d test.txt.asc<br />

To place the text in a file of your choice rather than the default name, you can use the -<br />

o flag as well <strong>and</strong> specify an output file like this:<br />

gpg –do test.out test.txt.asc<br />

Note that the output file is named first.<br />

If you have GPG set up so that the user your <strong>PHP</strong> scripts run as can use it from the<br />

comm<strong>and</strong> line, you are most of the way there. If this setup is not working, see your system<br />

administrator or the GPG documentation.<br />

Listings 18.1 <strong>and</strong> 18.2 enable people to send encrypted email by using <strong>PHP</strong> to call<br />

GPG.<br />

Listing 18.1<br />

private_mail.php— The HTML Form to Send Encrypted Email<br />

<br />

<br />

Send Me Private Mail<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!