2
我已經使用OpenPGP-PHP創建了公鑰和私鑰,然後我將它們兩者都導入到GnuPG中,它不會導入公鑰而不指定公鑰「--allow-non-self-signed-uid」標誌。然後,當我嘗試解密文件,我得到以下幾點:無法解密使用gnupg中的openpgp-php加密的文件
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 <[email protected]>",
'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 <[email protected]>",
'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成功解密我的東西?
「'$ RSA =新Crypt_RSA(); $ķ = $ rsa-> createKey(512);'「是否阻止RSA密鑰大小<2048位?(應該是) –
@ScottArciszewski將其提升到2048,仍然出現相同的錯誤!我將保留在2048,儘管如此, –
是$ public_bytes va可定義的riable? 'OpenPGP :: unarmor($ public_bytes,' –