【问题标题】:How do you use the PHP OpenPGP library?你如何使用 PHP OpenPGP 库?
【发布时间】:2012-09-13 17:32:31
【问题描述】:

gnupg PGP 库中有一个PHP extension port。但是,我很难 finding examples 解释如何使用该库。

您如何正确地为应用程序用户创建密钥,然后使用它们使用 GnuPG 库加密/解密文本?

【问题讨论】:

    标签: php gnupg openpgp


    【解决方案1】:

    看到这个 URL 对你很有帮助。下载示例并尝试一下。

    https://github.com/singpolyma/openpgp-php

    或者试试看:-

    您可以在网址上方下载lib/openpgp.phplib/openpgp_crypt_rsa.php文件。

    examples/keygen.php

    <?php
    
    require dirname(__FILE__).'/../lib/openpgp.php';
    require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';
    
    $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 <test@example.com>');
    
    $wkey = new OpenPGP_Crypt_RSA($nkey);
    $m = $wkey->sign_key_userid(array($nkey, $uid));
    
    print $m->to_bytes();
    

    examples/sign.php

    <?php
    
    require dirname(__FILE__).'/../lib/openpgp.php';
    require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';
    
    /* Parse secret key from STDIN, the key must not be password protected */
    $wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
    $wkey = $wkey[0];
    
    /* Create a new literal data packet */
    $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
    
    /* Create a signer from the key */
    $sign = new OpenPGP_Crypt_RSA($wkey);
    
    /* The message is the signed data packet */
    $m = $sign->sign($data);
    
    /* Output the raw message bytes to STDOUT */
    echo $m->to_bytes();
    
    ?>
    

    examples/verify.php

    <?php
    
    require dirname(__FILE__).'/../lib/openpgp.php';
    require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';
    
    /* Parse public key from STDIN */
    $wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
    $wkey = $wkey[0];
    
    /* Parse signed message from file named "t" */
    $m = OpenPGP_Message::parse(file_get_contents('t'));
    
    /* Create a verifier for the key */
    $verify = new OpenPGP_Crypt_RSA($wkey);
    
    /* Dump verification information to STDOUT */
    var_dump($verify->verify($m));
    
    ?>
    

    【讨论】:

    • 使用网上找到的随机 PHP 库进行加密,它从头开始重新实现 PGP,作者都不是密码学家,而且该库显然正在进行中(一半的函数只有一个//TODO 作为他们的身体)。会出什么问题?
    • @Tgr “最终的无知是拒绝你一无所知但拒绝调查的事情。”唯一可能对此造成危险的是对createKey() 的调用,它来自github.com/phpseclib/phpseclib/blob/master/phpseclib/Crypt/…,它调用,那是什么? OpenSSL。 OpenSSL 现在是“在网上找到的随机 PHP 库”?在编写这样的 cmets 之前,请先做一些研究。
    • @8chan,密钥生成实际上并不是加密中唯一可能包含危险错误的部分——加密也可以(我认为这很明显)。此外,如果 OpenSSL 不可用,phpseclib(与 openpgp-php 不同,确实得到了一些审查,尽管 not nearly as much as OpenSSL)会生成自己的密钥,并在纯 PHP 中重新实现实际加密。即使你有一个安全的加密原语,你也可以以不安全的方式应用它。
    • 我收到错误消息。致命错误:在第 2091 行的 /www/openpgp-php-master/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA.php 中的非对象上调用成员函数 equals()。有什么想法吗?
    • 您可以从供应商文件夹中的 composer 下载“phpseclib”库。
    【解决方案2】:

    它们是基于PHP extension port 的非常好的示例,您是否有要求,我们会看一些示例

    Using GnuPG with PHP -- Full Tutorials

    示例

    获取关键信息

    putenv('GNUPGHOME=/home/sender/.gnupg');
    
    // create new GnuPG object
    $gpg = new gnupg();
    
    // throw exception if error occurs
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION); 
    
    // get list of keys containing string 'example'
    try {
      $keys = $gpg->keyinfo('example');
      print_r($info);
    } catch (Exception $e) {
      echo 'ERROR: ' . $e->getMessage();
    }
    

    加密简单邮件

    // set path to keyring directory
    // set path to keyring directory
    putenv('GNUPGHOME=/home/sender/.gnupg');
    
    // create new GnuPG object
    $gpg = new gnupg();
    
    // throw exception if error occurs
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION); 
    
    // recipient's email address
    $recipient = 'dgar@example.org';
    
    // plaintext message
    $plaintext = 
    "Dear Dave,\n
      The answer is 42.\n
    John";
    
    // find key matching email address
    // encrypt plaintext message
    // display and also write to file
    try {
      $gpg->addencryptkey($recipient);
      $ciphertext = $gpg->encrypt($plaintext);
      echo '<pre>' . $ciphertext . '</pre>';
      file_put_contents('/tmp/ciphertext.gpg', $ciphertext);
    } catch (Exception $e) {
      die('ERROR: ' . $e->getMessage());
    }
    

    解密邮件

    // set path to keyring directory
    putenv('GNUPGHOME=/home/recipient/.gnupg');
    
    // create new GnuPG object
    $gpg = new gnupg();
    
    // throw exception if error occurs
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION); 
    
    // recipient's email address
    $recipient = 'dgar@example.org';
    
    // ciphertext message
    $ciphertext = file_get_contents('/tmp/ciphertext.gpg');
    
    // register secret key by providing passphrase
    // decrypt ciphertext with secret key
    // display plaintext message
    try {
      $gpg->adddecryptkey($recipient, 'guessme');
      $plaintext = $gpg->decrypt($ciphertext);
      echo '<pre>' . $plaintext . '</pre>';
    } catch (Exception $e) {
      die('ERROR: ' . $e->getMessage());
    }
    

    您还应该查看示例

    【讨论】:

      猜你喜欢
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      相关资源
      最近更新 更多