以下代码创建一个公钥/私钥对,使用 openssl_pkey_get_private() 函数成功打开密钥并将密钥对发送到显示器。然后将先前创建的密钥复制并粘贴回代码中。我尝试打开以前有效的完全相同的私钥,但现在它失败了。
有兴趣的可以从以下网址获取该库: http: //phpseclib.sourceforge.net/index.html
我正在使用昨天从源伪造网站下载的 phpseclib0.3.1。
问题是什么?
这是完整的代码。回显的结果已准备好进行复制和替换。
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../libraries/phpseclib');
include('../libraries/phpseclib/Net/SSH2.php');
include('../libraries/phpseclib/Crypt/RSA.php');
$rsa = new Crypt_RSA();
//$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
//$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
//define('CRYPT_RSA_EXPONENT', 65537);
//define('CRYPT_RSA_SMALLEST_PRIME', 64); // makes it so multi-prime RSA is used
extract($rsa->createKey()); // == $rsa->createKey(1024) where 1024 is the key size
echo("<h1 style=\"margin: 0; padding: 0;\"><b><u>Create Keys</u></b></h1><br />");
echo("\$privatekey2 = \"$privatekey\";<br /><br />");
echo("\$publickey2 = \"$publickey\";<br /><br />");
// Decrypt argument
define("KEY_PASSPHRASE", "");
$key = openssl_pkey_get_private($privatekey, KEY_PASSPHRASE);
if (FALSE == $key)
{
echo("Failed to get the private key<br />");
return false;
}
else
echo("Successfully obtained private key.<br />");
echo("<h1 style=\"margin: 0; padding: 15px 0px 0px 0px;\"><b><u>Read Copy and Pasted Keys</u></b></h1><br />");
$privatekey2 = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDPyrp/i8WSh8qmGHhTMaKgZeKj9X/Qh+8PrEFa8BqUMyZs61dGN9Cl9fKUq1iz cgRZWQsMPxNTmRvO4gWjBnkr1HQRELLywpfb6ecOin3uPeGY06Kme/DSRfS9YGOxy5rmhzNaCaSG WLdOUUFnQYCKdO5KkvqJSqnFYoLMBfLzEwIDAQABAoGBAJtAd1Ow1O+Ecc7j3ZMbNMzvEwU5+kOO LPni0/nkB5fPF9ithcm7DjPRrWuTEnUQrVssgmql/gSqEvLiQR/rSEA0jtgQ6n2m87SbWNz0DTLp j9jWFHbLFgo865IfssOiiht44FKWyMhQ9/FeH9gBQ0xTbHQIl8Hf/vm59n8wi3AhAkEA6fm6POv/ P+/u0Xz170FVcFVF8HosyPCiHTesdaf3U27Z8nxpPJmybf2+ZH7eJt5HUQAMvsJB75pe7gp7Iiwc awJBAONaCV5kfgmY1xGsRaWme21o1/wxqW8tX3/8glGQ1HeehiEnQ6krlNifrmeH2sBA+BpGhBGJ SovQKZA66TNFrfkCQEQpMAwXZCCoMRZuJOcyTUWQUfOgY/OVxmRkl6Ue7XoszOlLPQ0eaVCJnzF/ lBAWqQf7z7qytKqsegM2onBuEUkCQCmlY5Pz6paNddAZ84Qdk/x9uRrMahIgiJ7kPWb9lb33vvAx xhYmEpT/4y252Bthi1Ec27JuqMYlsodmEzO+LtkCQAVoE1zVkf/pQnUMcTyEE7vDAUouRjiXZHV7 JZq0uOu4ArQFhFBZILIfB9RYbb3Mr1mh1/pt+JipwtMuQ5Nnh7g= -----END RSA PRIVATE KEY-----";
$publickey2 = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPyrp/i8WSh8qmGHhTMaKgZeKj9X/Qh+8PrEFa 8BqUMyZs61dGN9Cl9fKUq1izcgRZWQsMPxNTmRvO4gWjBnkr1HQRELLywpfb6ecOin3uPeGY06Km e/DSRfS9YGOxy5rmhzNaCaSGWLdOUUFnQYCKdO5KkvqJSqnFYoLMBfLzEwIDAQAB -----END PUBLIC KEY-----";
echo("\$privatekey2 = \"$privatekey2\";<br /><br />");
$key2 = openssl_pkey_get_private($privatekey2, KEY_PASSPHRASE);
if (FALSE == $key2)
{
echo("Failed to get the copy and pasted key<br />");
return false;
}
else
echo("Successfully obtained private copy and pasted key.<br />");
?>
这是一个示例输出。此处的结果用作后续运行的输入(复制和粘贴)。
Create Keys
$privatekey2 = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDA5Ehy7A/nZ0CB+xikxods0MzID8OQfJof4nCiKAa7dEv4Z/FA5KpxVSPj+ZQj X+zdm7QLHfqOGTz6ac1eOtmBCmm1VtNh5cxRQwR00GV+PTpuaUjOghL/upcJ8RlC2UAkBMIlw+7O 3rZ+B5ykHSWF2fWyX9uk7N9Ls4LUjDLs7QIDAQABAoGAFp/wG4Kyztqeh2BzYIhkxA+tpV2r+5uR 3GGMAokdWQloC8ftVUY887Qf1JKmnIuY1dl4gcFPbTFqpJiaXQ4cCsFWMjYKcVrTZj38QaWp/ikc 4ZEuYfV6dr2FDUaQaIYlqPvCxmrc7eISHDZEvPNT9LUZcAtxzeJMTIXrbf6T9MECQQD0nH2fGglp FweVVjec/fp2coJuxiI3rtWuLP0Gy3a95d/UbSTxw+gXgVdTTSZy2FfHsTrc4J49jAbTYvVHqPj9 AkEAyd9Zep/jlbGOPIim+UTzvNJsQnJRC4jS1BsYjP/digMkNDQG/nt4R2QZVlTHXM7+abdbeXYA yW3AOPHzP8++sQJBAMJHmK7JKzhAhlFiDQGv50I0aMw6qeq3/hc8vCqtZz4kWcaKeZFcG/avUABl gmgs1GtSpIHSFVyk3rb+HfowhrECQBRFdmHG7n6YrtTLUCEV2HfslWxyuEq1OZ6M/gEirGdwe1E2 rEEfuutIVPcDWmWqb4fEHs5qmBeiCrxtJ2UQAnECQCoUFle3/XAll4sACiV5/Endd7DKCM1g6jQd rtCbvzHh/j6P3sZEGuTZcuDWYmtNVUy6E2HHfNNwztZxVEJZqW4= -----END RSA PRIVATE KEY-----";
$publickey2 = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDA5Ehy7A/nZ0CB+xikxods0MzID8OQfJof4nCi KAa7dEv4Z/FA5KpxVSPj+ZQjX+zdm7QLHfqOGTz6ac1eOtmBCmm1VtNh5cxRQwR00GV+PTpuaUjO ghL/upcJ8RlC2UAkBMIlw+7O3rZ+B5ykHSWF2fWyX9uk7N9Ls4LUjDLs7QIDAQAB -----END PUBLIC KEY-----";
Successfully obtained private key.
Read Copy and Pasted Keys
Failed to get the copy and pasted key
更新 1: phpseclib0.3.1 似乎没有该更改,但是更改并没有解决问题,至少从复制和粘贴来看。我手动更改了 RSA.php(v0.3.1 上的第 795-804 行):
$RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
"Proc-Type: 4,ENCRYPTED\r\n" .
"DEK-Info: DES-EDE3-CBC,$iv\r\n" .
"\r\n" .
chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) .
'-----END RSA PRIVATE KEY-----';
} else {
$RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
chunk_split(base64_encode($RSAPrivateKey), 64) .
'-----END RSA PRIVATE KEY-----';
更新 2:我得到了工作的代码。我不确定上面是否包含 ", 64" 是否起到了作用或是否需要,但诀窍是用 "\n" 替换看起来像空格的字符,可能来自复制和粘贴作业. 一旦我这样做了,那么瞧!可能需要两次编辑。