【问题标题】:Can't decrypt file encrypted with openpgp-php in gnupg无法在gnupg中解密用openpgp-php加密的文件
【发布时间】:2016-06-08 14:01:38
【问题描述】:

我已经使用 OpenPGP-PHP 创建了一个公钥和私钥,然后我将它们都导入了 GnuPG,如果不指定“--allow-non-selfsigned-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 成功解密我的东西?

【问题讨论】:

  • "$rsa = new Crypt_RSA(); $k = $rsa-&gt;createKey(512);" 是否阻止 RSA 密钥大小
  • @ScottArciszewski 将其提高到 2048 年,但仍然出现相同的错误!不过,我会保持在 2048 年。
  • 是否定义了 $public_bytes 变量? OpenPGP::unarmor( $public_bytes,
  • 可能与此相关:github.com/singpolyma/openpgp-php/issues/27 使用生成的密钥对使用 gpg 加密不起作用(引发错误并使 GPA 崩溃)(公钥不显示,私钥只有证书能力)。解密也表示没有有效的加密 PGP 数据。缺少加密/解密 (E) 功能。您是否尝试过 gpg --edit-key keyid ?所以你没有遗漏什么,这是生成的密钥对的问题。

标签: php encryption gnupg pgp


【解决方案1】:

首先:尝试使用由 gpg 生成的密钥通过 OpenPGP-PHP 进行加密——这行得通吗? (它在本地对我有用。)

接下来,确保您生成的密钥设置了 0x04“加密通信”密钥标志(您必须自己为此添加签名包,请查看第 154 行的lib/openpgp_crypt_rsa.php 以查看默认生成的包如果你不提供。

此外,keygen 示例中存在错误。请尝试最新版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多