我有一个使用OpenSSL进行RSA解密的PHP程序,当我在远程Linux(Ubuntu)网络服务器上运行它时可以正常工作,但是当我在Windows下运行它时失败(从PHPstorm启动,使用PHP构建) -in web server)。
我在Windows下使用OpenSSL生成了私钥:
openssl genrsa -out private.pem 2048
这是在Linux下运行的PHP代码,但在Windows下失败:
$req = "R6lnBb8Hhu5YEmVvdJKUEzckq5afHGFauRca3q6+NuPf1Jrx0+5WtCFj+uFrCBkQtu7sqCoZ+nIh\n" .
"oLqn5GHn6rLAI03kiB3USrV232LKiamzepk1mHxaMQ+q+um95XETnuCrDfH+ufTsErNo2IjhAGjF\n" .
"6SxQxA00+HpMBtu/lSKCz2x67rG3g4zdhnUXRsBlO3T0iX8S0loudklc34Phfa/4EqR14Vcb0Z7b\n" .
"Z1UdLxYJEhW32Rnk4iXZV0ChpkiNdSTOH+GfRJpKR0VwdbfuBGDtV8ktzxiNsNFaA/n4XNKPB2LU\n" .
"9CCyi+s6zYbXvayxFTcVPVMxM0zBOfa45j9nYw==";
$cryptext = base64_decode($req);
$path = 'file:///' . __DIR__ . DIRECTORY_SEPARATOR . 'private.pem';
// file_exists($path) confirmed to return true when run on both platforms.
$res = openssl_pkey_get_private($path);
openssl_pkey_export($res, $privatekey);
echo "\nPrivate Key:$privatekey\n<HR>\n";
openssl_private_decrypt($cryptext, $decrypted, $privatekey);
echo("$req\n<HR>\n");
echo("$decrypted");
(正确)在LINUX服务器上运行时的输出:
file exists
<hr>
Private Key:-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDfm6Ik7Z6Oj3ud
2xslm+CP0mzAWmO2z5ENC/KEOwi31hn9l63bsMhznDufr3uy7EEN8P4TJTt8YltX
cQxKil/JAFyL8zol1vQ6EtHQkQvDbh9I0r5Ldd7+PuiyyQIiqrEY3TSKANrbUU3k
N5vSU9BbBULlyhpFa4QCi7+A6O0SNUrn60Ew9YBEmf4Ogka1Vw9qRY/2qSTJCWJ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XUgYZTWxoM+sk5FUjBZDKX
35D0OwvVyE/qTfpAI6aXAvsayqajP68JLSGiZ262FX30cjsXO/tU9qtxX9KvM44x
oPXp5cOR5dOhZPF8DuUH3utQhVsWnoBaj/kr9cECgYEAq+SN+0UYDBr+UKKHjZkk
b6WWt9WOUpD/hJOWGAcl9v+03VP6NK3zje2ky9fVxyJTyH/MGbg4G1REG27Is7tr
hGKXmqpyZyIDc2vJDrDB0XrkaDP41b/by7x1CdXUr06wTvMC0XpnJvh0CGczjMDO
n0+0L6CEpfT/KZOz2LyqyUw=
-----END PRIVATE KEY-----
<hr>
R6lnBb8Hhu5YEmVvdJKUEzckq5afHGFauRca3q6+NuPf1Jrx0+5WtCFj+uFrCBkQtu7sqCoZ+nIh
oLqn5GHn6rLAI03kiB3USrV232LKiamzepk1mHxaMQ+q+um95XETnuCrDfH+ufTsErNo2IjhAGjF
6SxQxA00+HpMBtu/lSKCz2x67rG3g4zdhnUXRsBlO3T0iX8S0loudklc34Phfa/4EqR14Vcb0Z7b
Z1UdLxYJEhW32Rnk4iXZV0ChpkiNdSTOH+GfRJpKR0VwdbfuBGDtV8ktzxiNsNFaA/n4XNKPB2LU
9CCyi+s6zYbXvayxFTcVPVMxM0zBOfa45j9nYw==
<hr>
CONNECT=dd924b237b1366dafa99a112dc053344
(失败)在WINDOWS(PHPstorm,PHP的内置Web服务器)下运行时输出
C:\bin\php\php.exe C:\src\BTNcrypt-PHP\e2ncrypt\do.php
PHP Warning: openssl_pkey_export(): cannot get key from parameter 1 in C:\src\BTNcrypt-PHP\e2ncrypt\do.php on line 23
string(28) "C:\src\BTNcrypt-PHP\e2ncrypt"
file exists
PHP Warning: openssl_private_decrypt(): key parameter is not a valid private key in C:\src\BTNcrypt-PHP\e2ncrypt\do.php on line 25
<HR>
Warning: openssl_pkey_export(): cannot get key from parameter 1 in C:\src\BTNcrypt-PHP\e2ncrypt\do.php on line 23
Private Key:
<HR>
Warning: openssl_private_decrypt(): key parameter is not a valid private key in C:\src\BTNcrypt-PHP\e2ncrypt\do.php on line 25
R6lnBb8Hhu5YEmVvdJKUEzckq5afHGFauRca3q6+NuPf1Jrx0+5WtCFj+uFrCBkQtu7sqCoZ+nIh
oLqn5GHn6rLAI03kiB3USrV232LKiamzepk1mHxaMQ+q+um95XETnuCrDfH+ufTsErNo2IjhAGjF
6SxQxA00+HpMBtu/lSKCz2x67rG3g4zdhnUXRsBlO3T0iX8S0loudklc34Phfa/4EqR14Vcb0Z7b
Z1UdLxYJEhW32Rnk4iXZV0ChpkiNdSTOH+GfRJpKR0VwdbfuBGDtV8ktzxiNsNFaA/n4XNKPB2LU
9CCyi+s6zYbXvayxFTcVPVMxM0zBOfa45j9nYw==
<HR>
Process finished with exit code 0
我非常确定这主要是由于特定于Windows的funkiness超出了0x0D 0x0A换行符与0x0A换行符...作为实验,我将.pem文件转换为使用Windows风格的换行符,但在Windows下仍然失败。