GithubHelp home page GithubHelp logo

Comments (2)

efinder2 avatar efinder2 commented on August 14, 2024

Same Problem here.
I've tried it with the following code:

<?php
ini_set('xdebug.var_display_max_depth', 5);
ini_set('xdebug.var_display_max_children', 256);
ini_set('xdebug.var_display_max_data', 10000);
require_once 'vendor/autoload.php';
 $message = 'Test Text';
$yourPassphrase = 'Passphrase';
$pathToYourAsciiArmoredPublicKey = __DIR__.'/../keys/public.key';
$pathToYourAsciiArmoredPrivateKey = __DIR__.'/../keys/private.key';
$key = OpenPGP_Message::parse(
	OpenPGP::unarmor(file_get_contents($pathToYourAsciiArmoredPublicKey), 'PGP PUBLIC KEY BLOCK')
);
$data = new OpenPGP_LiteralDataPacket($message);
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
$encryptedPrivateKey = OpenPGP_Message::parse(
	OpenPGP::unarmor(file_get_contents($pathToYourAsciiArmoredPrivateKey), 'PGP PRIVATE KEY BLOCK')
);
var_dump($encrypted);
var_dump(OpenPGP::enarmor($encrypted->packets[0]->encrypted_data));
// Try each secret key packet
foreach ($encryptedPrivateKey as $package) {
	if (!($package instanceof OpenPGP_SecretKeyPacket)) {
		continue;
	}
	$key = OpenPGP_Crypt_Symmetric::decryptSecretKey($yourPassphrase, $package);
	$decryptor = new OpenPGP_Crypt_RSA($key);
	$decrypted = $decryptor->decrypt($encrypted);
	echo $decrypted;
}

from openpgp-php.

singpolyma avatar singpolyma commented on August 14, 2024

@efinder2 I have found a bug that was causing your code to fail, it is fixed in master now so if you try again it should work.

You may be thinking "if there was such a bug, how did this ever work?" -- well, the following version of your code will work both on the old version of the library, and the new one:

<?php
require_once '/usr/share/php/phpseclib/autoload.php';
require_once 'lib/openpgp_crypt_rsa.php';
$message = 'Test Text';
$yourPassphrase = 'test';
$pathToYourAsciiArmoredPublicKey = './pub.asc';
$pathToYourAsciiArmoredPrivateKey = './sekret.asc';
$key = OpenPGP_Message::parse(
	OpenPGP::unarmor(file_get_contents($pathToYourAsciiArmoredPublicKey), 'PGP PUBLIC KEY BLOCK')
);
$data = new OpenPGP_LiteralDataPacket($message);
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
$encryptedPrivateKey = OpenPGP_Message::parse(
	OpenPGP::unarmor(file_get_contents($pathToYourAsciiArmoredPrivateKey), 'PGP PRIVATE KEY BLOCK')
);
var_dump($encrypted);
var_dump(OpenPGP::enarmor($encrypted->packets[0]->encrypted_data));
// Try each secret key packet

$decryptedPrivateKey = new OpenPGP_Message(array_map(function($packet) use ($yourPassphrase) {
	return OpenPGP_Crypt_Symmetric::decryptSecretKey($yourPassphrase, $packet);
}, array_filter(
	$encryptedPrivateKey->packets,
	function($packet) { return $packet instanceof OpenPGP_SecretKeyPacket;}
)));

$decryptor = new OpenPGP_Crypt_RSA($decryptedPrivateKey);
$decrypted = $decryptor->decrypt($encrypted);
var_dump($decrypted);

So thanks for trying a different way and finding this bug! I have added a test case for it so it should not come back.

from openpgp-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.