DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Test Message For Valid PGP Encryption

  • submit to reddit
        This code is designed to check whether the parsed text is a valid PGP encrypted message.  It's relatively easy to fool, but not much can be done about that.

# vim: ft=php

function is_pgp($my_string) {

$begin =  strpos($my_string, "-----BEGIN PGP");
if ($begin === false) {
 return false;

$sign = strpos($my_string, "----BEGIN PGP SIGN");
if ($sign !== false) {
 return false;

$blank = strpos($my_string, "\r\n\r\n");
if ($blank === false) {
 return false;

$end = strpos($my_string, "----END");
if ($end === false) {
 return false;
if ($end < $begin) {
 return false;

return true;


if ($_POST['message'] != "") {
if(is_pgp($_POST['message'])==true) {
 echo "I think the following text is a valid pgp encrypted message";
 } else {
 echo "The following text does not validate as a pgp encrypted message";
} else {echo "use the following form to test whether text is pgp encrypted"; }

<form action=pgp.php method="post">
<textarea name="message"  rows="20" cols="60" wrap="virtual">
<?php echo($_POST['message']); ?>
<input type="submit" value="validate text">