<?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); }
<?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 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.'); }
/** */ public function decrypt($msg, $key) { $decryptor = new OpenPGP_Crypt_RSA($key->message); $elgamal = null; foreach ($msg->message as $val) { if ($val instanceof OpenPGP_AsymmetricSessionKeyPacket) { $pkey = $decryptor->key($val->keyid); if (!$pkey instanceof OpenPGP_PublicKeyPacket) { continue; } switch ($pkey->algorithm) { case 1: case 2: return new Horde_Pgp_Element_Message($decryptor->decrypt($msg->message)); case 16: $elgamal = new Horde_Pgp_Crypt_Elgamal($pkey); /* Put encrypted data into a packet object to take * advantage of built-in MPI read methods. */ $edata = new OpenPGP_Packet(); $edata->input = $val->encrypted_data; $sk_data = $elgamal->decrypt($edata->read_mpi() . $edata->read_mpi()); $sk = substr($sk_data, 1, strlen($sk_data) - 3); /* Last 2 bytes are checksum */ $chk = unpack('n', substr($sk_data, -2)); $chk = reset($chk); $sk_chk = 0; for ($i = 0, $j = strlen($sk); $i < $j; ++$i) { $sk_chk = ($sk_chk + ord($sk[$i])) % 65536; } if ($sk_chk != $chk) { throw new RuntimeException(); } return new Horde_Pgp_Element_Message(OpenPGP_Crypt_Symmetric::decryptPacket(OpenPGP_Crypt_Symmetric::getEncryptedData($msg->message), ord($sk_data[0]), $sk)); } } } throw new RuntimeException(); }