【问题标题】:How to generate Public and Private Key Pair? [closed]如何生成公钥和私钥对? [关闭]
【发布时间】:2014-10-11 20:17:50
【问题描述】:

我已经阅读了有关 PHP 中非对称加密的所有内容。我了解 mcrypt 方法。我了解拥有两个密钥(公共和私人)的要求。我唯一不明白的是我在哪里以及如何生成这些密钥对?谁能解释一下密钥生成的过程? 谢谢!

【问题讨论】:

  • 您具体在哪里阅读了关于非对称加密的所有内容mcrypt?它only spports symetric 密码。
  • 对不起,我并不是要暗示 mcrypt 需要两个密钥。我的意思是我已经阅读了对称和非对称加密。我已经编辑了问题。
  • 查看openssl_pkey_new,或使用命令行openssl工具生成密钥对。

标签: php encryption-asymmetric


【解决方案1】:

你需要a PGP library 来做你想做的事,但这很简单。要创建您的密钥,请使用:

<?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();

【讨论】:

  • 感谢您的回复。不过还有一个问题,我是否需要从某个地方下载库文件(openpgp.php 和 openpgp_crypt_rsa.php)?因为代码对这些行给出了以下错误: Failed opening required '/home/content/27/11042427/html/../lib/openpgp.php
  • 您需要从here 安装必要的PHP 文件。再次祝您好运,如果您还有其他问题,请留下问题
猜你喜欢
  • 2012-04-05
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多