OpenPGP_Packet::read_mpi PHP Method

read_mpi() public method

See also: http://tools.ietf.org/html/rfc4880#section-3.2
public read_mpi ( )
    function read_mpi()
    {
        $length = $this->read_unpacked(2, 'n');
        // length in bits
        $length = (int) floor(($length + 7) / 8);
        // length in bytes
        return $this->read_bytes($length);
    }

Usage Example

Exemplo n.º 1
0
 /**
  */
 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();
 }