/** * Constructor. * * @param mixed $data Data of the part. Either raw PGP data or a * OpenPGP_Message object. * @param array $headers Header array. */ public function __construct($data, array $headers = array()) { if (!$data instanceof OpenPGP_Message) { Horde_Pgp_Backend_Openpgp::autoload(); $data = OpenPGP_Message::parse($data); } $this->message = $data; $this->headers = $headers; }
static function convert_key($packet, $private = false) { if (!is_object($packet)) { $packet = OpenPGP_Message::parse($packet); } if ($packet instanceof OpenPGP_Message) { $packet = $packet[0]; } $mod = $packet->key['n']; $exp = $packet->key['e']; if ($private) { $exp = $packet->key['d']; } if (!$exp) { return NULL; } // Packet doesn't have needed data $rsa = self::crypt_rsa_key($mod, $exp); if ($private) { if ($packet->key['p'] && $packet->key['q']) { $rsa->primes = array($packet->key['p'], $packet->key['q']); } if ($packet->key['u']) { $rsa->coefficients = array($packet->key['u']); } } return $rsa; }
function read() { $this->algorithm = ord($this->read_byte()); $this->data = $this->read_bytes($this->length); switch ($this->algorithm) { case 0: $this->data = OpenPGP_Message::parse($this->data); break; case 1: $this->data = OpenPGP_Message::parse(gzinflate($this->data)); break; case 2: $this->data = OpenPGP_Message::parse(gzuncompress($this->data)); break; case 3: $this->data = OpenPGP_Message::parse(bzdecompress($this->data)); break; default: /* TODO error? */ } }
<?php require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; /* Parse secret key from STDIN, the key must not be password protected */ $wkey = OpenPGP_Message::parse(file_get_contents('php://stdin')); $wkey = $wkey[0]; /* Create a new literal data packet */ $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); /* Create a signer from the key */ $sign = new OpenPGP_Crypt_RSA($wkey); /* The message is the signed data packet */ $m = $sign->sign($data); /* Output the raw message bytes to STDOUT */ echo $m->to_bytes();
public static function decryptPacket($epacket, $symmetric_algorithm, $key) { list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($symmetric_algorithm); if (!$cipher) { return null; } $cipher->setKey($key); if ($epacket instanceof OpenPGP_IntegrityProtectedDataPacket) { $padAmount = $key_block_bytes - strlen($epacket->data) % $key_block_bytes; $data = substr($cipher->decrypt($epacket->data . str_repeat("", $padAmount)), 0, strlen($epacket->data)); $prefix = substr($data, 0, $key_block_bytes + 2); $mdc = substr(substr($data, -22, 22), 2); $data = substr($data, $key_block_bytes + 2, -22); $mkMDC = hash("sha1", $prefix . $data . "Ó", true); if ($mkMDC !== $mdc) { return false; } try { $msg = OpenPGP_Message::parse($data); } catch (Exception $ex) { $msg = null; } if ($msg) { return $msg; } /* Otherwise keep trying */ } else { // No MDC mean decrypt with resync $iv = substr($epacket->data, 2, $key_block_bytes); $edata = substr($epacket->data, $key_block_bytes + 2); $padAmount = $key_block_bytes - strlen($edata) % $key_block_bytes; $cipher->setIV($iv); $data = substr($cipher->decrypt($edata . str_repeat("", $padAmount)), 0, strlen($edata)); try { $msg = OpenPGP_Message::parse($data); } catch (Exception $ex) { $msg = null; } if ($msg) { return $msg; } /* Otherwise keep trying */ } return null; /* Failed */ }
<?php // USAGE: php examples/deASCIIdeCrypt.php secretkey.asc password message.asc // This will fail if the algo on key or message is not 3DES or AES require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_symmetric.php'; $keyASCII = file_get_contents($argv[1]); $msgASCII = file_get_contents($argv[3]); $keyEncrypted = OpenPGP_Message::parse(OpenPGP::unarmor($keyASCII, 'PGP PRIVATE KEY BLOCK')); // Try each secret key packet foreach ($keyEncrypted as $p) { if (!$p instanceof OpenPGP_SecretKeyPacket) { continue; } $key = OpenPGP_Crypt_Symmetric::decryptSecretKey($argv[2], $p); $msg = OpenPGP_Message::parse(OpenPGP::unarmor($msgASCII, 'PGP MESSAGE')); $decryptor = new OpenPGP_Crypt_RSA($key); $decrypted = $decryptor->decrypt($msg); var_dump($decrypted); }
public function testEncryptAsymmetric() { $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data))); $decryptor = new OpenPGP_Crypt_RSA($key); $decrypted = $decryptor->decrypt($encrypted); $this->assertEquals($decrypted[0]->data, 'This is text.'); }
<?php require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; /* Parse public key from STDIN */ $wkey = OpenPGP_Message::parse(file_get_contents('php://stdin')); /* Parse signed message from file named "t" */ $m = OpenPGP_Message::parse(file_get_contents('t')); /* Create a verifier for the key */ $verify = new OpenPGP_Crypt_RSA($wkey); /* Dump verification information to STDOUT */ var_dump($verify->verify($m));
<?php require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_symmetric.php'; $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/../tests/data/helloKey.gpg')); $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data))); // Now decrypt it with the same key $decryptor = new OpenPGP_Crypt_RSA($key); $decrypted = $decryptor->decrypt($encrypted); var_dump($decrypted);
public function signDocument() { $document = new \StdClass(); foreach ($this->resourceData as $term => $value) { $document->{$term} = $this->normalizeData($value); } unset($document->digital_signature); unset($document->_id); unset($document->_rev); unset($document->doc_id); unset($document->publishing_node); unset($document->update_timestamp); unset($document->node_timestamp); unset($document->create_timestamp); $bencoder = new \LearningRegistry\Bencode\LearningRegistryBencodeEncoderTrial(); $document = (array) $document; $bencodedDocument = utf8_encode($bencoder->encode($document)); $hashedDocument = hash('SHA256', $bencodedDocument); global $loader; if (!isset($loader)) { $loader = $this->getLoader(); } spl_autoload_unregister(array($loader, 'loadClass')); require_once dirname(__FILE__) . '/../OpenPGP/openpgp.php'; require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_rsa.php'; require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_symmetric.php'; $keyASCII = $this->getKey(); $keyEncrypted = \OpenPGP_Message::parse(\OpenPGP::unarmor($keyASCII, 'PGP PRIVATE KEY BLOCK')); foreach ($keyEncrypted as $p) { if (!$p instanceof \OpenPGP_SecretKeyPacket) { continue; } $key = \OpenPGP_Crypt_Symmetric::decryptSecretKey($this->getPassPhrase(), $p); } $data = new \OpenPGP_LiteralDataPacket($hashedDocument, array('format' => 'u')); $sign = new \OpenPGP_Crypt_RSA($key); $m = $sign->sign($data); $packets = $m->signatures()[0]; $message = "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\n"; $message .= $packets[0]->data . "\n"; $message .= "-----BEGIN PGP SIGNATURE-----\n\n"; $signed_data = str_replace("-----BEGIN -----", "", str_replace("-----END -----", "", \OpenPGP::enarmor($packets[1][0]->to_bytes(), ""))); $signature = str_split(trim($signed_data), 65); foreach ($signature as $line) { $message .= $line . "\n"; } $message .= "-----END PGP SIGNATURE-----\n"; $this->setSigFields(array('signature' => $message, 'key_owner' => $this->getKeyOwner(), 'key_location' => array($this->getPublicKeyPath()), 'signing_method' => "LR-PGP.1.0")); spl_autoload_register(array($loader, 'loadClass')); $this->document = $this->createDocument(); }
public function oneFingerprint($path, $kf) { $m = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path)); $this->assertEquals($m[0]->fingerprint(), $kf); }