Can't decrypt files encrypted with OpenPGP.php
dandys opened this issue · 5 comments
dandys commented
When I try to encrypt some small text, GPG can decrypt this file without problem:
require_once dirname(__FILE__).'/openpgp-php/vendor/autoload.php';
$message = 'Test message';
$key = OpenPGP_Message::parse(
OpenPGP::unarmor(file_get_contents('public.asc'), 'PGP PUBLIC KEY BLOCK')
);
$data = new OpenPGP_LiteralDataPacket($message);
$encrypted = OpenPGP_Crypt_Symmetric::encrypt(
$key,
new OpenPGP_Message(array($data))
);
file_put_contents('output.txt.asc', OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'));
However, when I set $message variable with:
$message = file_get_contents('test.pdf');
or any other bigger text file, GPG (I use KGPG frontend) shows an error with decryption.
What causes an error?
I attach test.pdf file to this issue.
test.pdf
singpolyma commented
$data = new OpenPGP_LiteralDataPacket($message);
...
$message = file_get_contents('test.pdf');
I believe the default LiteralDataPacket constructor assumes Unicode content -- you will want to pass second or third argument as 'b' to set binary mode and prevent line-ending normalisation and expectation of UTF-8 bytes.
dandys commented
You mean second argument of LiteraDataPacket? Here's the constructor:
function __construct($data=NULL, $opt=array()) {
parent::__construct();
$this->data = $data;
$this->format = isset($opt['format']) ? $opt['format'] : 'b';
$this->filename = isset($opt['filename']) ? $opt['filename'] : 'data';
$this->timestamp = isset($opt['timestamp']) ? $opt['timestamp'] : time();
}
So 'b' is the default value of 'format' option.
singpolyma commented
So 'b' is the default value of 'format' option.
Ok, I must have misremembered. Will look into reproducing this when I get some time.
dandys commented
Sure. If you need any additional informations, let me know.
singpolyma commented
This appears to be a bug in the enarmor
code, it works fine without that.