singpolyma / openpgp-php

OpenPGP.php is a pure-PHP implementation of the OpenPGP Message Format (RFC 4880).
http://singpolyma.github.io/openpgp-php/
The Unlicense
179 stars 69 forks source link

Cant decrypt on CLI #113

Open Petah opened 2 years ago

Petah commented 2 years ago

Hi,

When trying to decrypt the output of this lib using gpg cli tool I get this:

$ gpg --decrypt test.enc                                    
gpg: assuming IDEA encrypted data
gpg: [don't know]: indeterminate length for invalid packet type 2
gpg: WARNING: encrypted message has been manipulated!
gpg: [don't know]: invalid packet (ctb=40)

This is the PHP code

$pk = OpenPGP_Message::parse(file_get_contents('public.asc'));
$data = new OpenPGP_LiteralDataPacket("some text\n", ['format' => 'u']);
$encrypted = OpenPGP_Crypt_Symmetric::encrypt([$pk], new OpenPGP_Message([$data]));
echo OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE');
markkimsal commented 2 years ago

The key itself needs to be un-armored and parsed

  $pubkey = './test-public-key.asc';
  $pk = OpenPGP_Message::parse(\OpenPGP::unarmor(file_get_contents($pubkey), 'PGP PUBLIC KEY BLOCK'));
  foreach ($pk->packets as $p) { 
      if (!($p instanceof \OpenPGP_PublicKeyPacket)) { 
          continue;
      } 
      $publickeyPacket = $p;
  }
  $data = new OpenPGP_LiteralDataPacket("some text\n", ['format' => 'u']);
  $encrypted = OpenPGP_Crypt_Symmetric::encrypt([$publickeyPacket], new OpenPGP_Message([$data]));                                                                    
  echo OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE');