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 */
}