/** * Return the unencrypted version of the private key. * * @param string $passphrase The passphrase used to encrypt the key. * * @return Horde_Pgp_Element_PrivateKey Unencrypted key. * @throws Horde_Pgp_Exception */ public function getUnencryptedKey($passphrase = null) { $out = null; foreach ($this->_getSecretKeyPackets() as $k => $v) { if (!strlen($v->encrypted_data)) { continue; } if (is_null($out)) { $out = clone $this->message; } $out[$k] = OpenPGP_Crypt_Symmetric::decryptSecretKey($passphrase, $v); if (is_null($out[$k])) { throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Could not unencrypt private key.")); } } return new Horde_Pgp_Element_PrivateKey($out); }
public function testDecryptSecretKey() { $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/encryptedSecretKey.gpg')); $skey = OpenPGP_Crypt_Symmetric::decryptSecretKey("hello", $key[0]); $this->assertSame(!!$skey, true); }
<?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 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(); }