以下代码创建公钥/私钥对,使用openssl_pkey_get_private()函数成功打开密钥,并将密钥对发送到显示器。然后将先前创建的密钥复制并粘贴回代码中。我尝试打开同样精确的私钥,它之前有效,现在它失败了。
对于有兴趣的人,您可以在以下地址获取图书馆: http://phpseclib.sourceforge.net/index.html
我使用昨天从source forge网站下载的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进行了更改(第795行第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” 。我一做到那就瞧!可能需要两次编辑。
答案 0 :(得分:1)
您使用的是最新的Git版本的phpseclib吗?我问,因为不久前有这个提交: