我已经使用OpenPGP-PHP创建了一个公钥和私钥,然后我将它们都导入到GnuPG中,GnuPG不会在不指定&#34的情况下导入公钥。 -ALLOW类非自署名的uid"旗。然后,当我尝试解密文件时,我得到以下内容:
gpg --decrypt test.tif.asc
Generated: Thu, 25 Feb 2016 11:12:28 -0500
gpg: unknown armor header:
By: <>
gpg: unknown armor header:
Using: OpenPGP for PHP
gpg: unknown armor header:
For: TEST
gpg: unknown armor header:
gpg: encrypted with 512-bit RSA key, ID 519A1973, created 2016-02-25
"TEST"
gpg: public key decryption failed: Wrong secret key used
gpg: decryption failed: No secret key
但关键显然存在:
gpg --list-secret-keys
/home/mike/.gnupg/secring.gpg
-----------------------------
sec 512R/519A1973 2016-02-25
uid TEST
我使用PHP代码生成它们:
$header = array(
'Generated' => date( "r" ),
'By' => "Test <test@test.com>",
'Using' => "OpenPGP for PHP",
'For' => "TEST"
);
$rsa = new Crypt_RSA();
$k = $rsa->createKey(512);
$rsa->loadKey( $k['privatekey'] );
$nkey = new OpenPGP_SecretKeyPacket(array(
'n' => $rsa->modulus->toBytes(),
'e' => $rsa->publicExponent->toBytes(),
'd' => $rsa->exponent->toBytes(),
'p' => $rsa->primes[1]->toBytes(),
'q' => $rsa->primes[2]->toBytes(),
'u' => $rsa->coefficients[2]->toBytes()
));
$uid = new OpenPGP_UserIDPacket( "TEST" );
$wkey = new OpenPGP_Crypt_RSA ($nkey );
$m = $wkey->sign_key_userid(array($nkey, $uid));
// Serialize private key
$private_bytes = $m->to_bytes();
$private_bytes = OpenPGP::enarmor( $private_bytes, "PGP PRIVATE KEY BLOCK", $header );
// Serialize public key message
$pubm = clone($m);
$pubm[0] = new OpenPGP_PublicKeyPacket($pubm[0]);
$public_bytes = OpenPGP::enarmor( $public_bytes, "PGP PUBLIC KEY BLOCK", $header );
使用以下方式加密:
$pgp_header = array(
'Generated' => date( "r" ),
'By' => "Test <test@test.com>",
'Using' => "OpenPGP for PHP",
'For' => "TEST"
);
// Unarmor the public key for encrypting
$public_bytes = OpenPGP_Message::parse( OpenPGP::unarmor( $public_bytes, "PGP PUBLIC KEY BLOCK" ) );
echo encryptData( file_get_contents( "test.tif" ), "test.tif", $public_bytes, $pgp_header );
function encryptData( $data, $filename, $key, $header )
{
$data = new OpenPGP_LiteralDataPacket( $data, array('format' => 'u', 'filename' => $filename ) );
$encrypted = OpenPGP_Crypt_Symmetric::encrypt( $key, new OpenPGP_Message(array($data)) );
return( OpenPGP::enarmor( $encrypted->to_bytes(), "PGP MESSAGE", $header ) );
}
但似乎我肯定在某处遗漏了某些东西。如何让GnuPG成功解密我的东西?
答案 0 :(得分:1)
首先:尝试使用使用gpg生成的密钥使用OpenPGP-PHP进行加密 - 这有用吗? (它在当地为我做。)
接下来,确保您生成的密钥设置了0x04“加密通信”密钥标记(您必须自己添加签名包,请在第154行查看lib/openpgp_crypt_rsa.php
以查看默认值 - 如果您不提供数据包,则生成数据包。
此外,keygen示例中存在错误。请尝试最新版本。