PHP中解密数据的私钥无效

时间:2015-03-09 06:10:50

标签: php encryption openssl rsa

我正在尝试解密PHP中的私钥数据。但我的响应显示私钥无效。我尝试了很多其他选择,但没有帮助。首先,我至少需要一个如何在PHP中解密数据的最简单的例子。我的代码:

$privateKey = openssl_get_privatekey(base64_encode('-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQCK0ddPrH6Sr329AEgS1MzXrnkKliLOzSOLtVIlzpzhuaRznPbM
6u1QJSNiNspDyEe66fv6vSlceWwDgAb99YiYnpis6DUHsbrlRbvC25TEsGbaaI95
v+Y/vizUMPVgxJT73JsXZOaH6RfOGvXZFec76knveTa5X9bTT+AAm+W1EwIDAQAB
AoGBAIMTWkM/s4/bplUkg+lMB1MLyfymAJ3V8t9CyRsruNQmDtKffiXFGWjYeE2P
QHK6FIi30JZTsE9B0yyG4ZNAGMXmLws2eqCUu3j+zfTeQ6Ir/fmndXsZNFoJyPBn
Ofe3YQ2O+cHd0g5w9oVBLEej+vI5m29+Vd3KStA78jhOHoNxAkEAyB+8hN4XQTD2
nssiImpprLZco/fMlTcDFP+tDDaKskvZpxgmG1KDSn2wDAmkk0avS07Z4ht97Yog
FK4X4jN7GQJBALGUQgh+CqiDBk1SJ3e8tidJlvXWpJrA1Jtt523pERE+flwnVYUN
N8TcZAyCNEkpg+1Mmlvr3hQYF5p77k8TIwsCQQCtHWcCuOv85NIyAx6ADvVjBk2V
OaKr4W61F4cmbXgFTD/3n5g3QmV4ZsmHYEARdq/kwU0Rnx6MAVRHD2Jxtn7hAkEA
klGG11iDj7XmS2+V9aMYs+Z+7LGfj6Ozix5Twsx3s626kFQYszjg6uRty6C9VEtQ
2QZ1aag/pG71g+T5x04NYQJAXqOsA45bHcW0PfRKEXdiQoaSF5E//B5DyqQxPUfb
r7AxSx/r9ch5TCnhn4JffhE326QTYF65QgxAo61rFrsnrA==
-----END RSA PRIVATE KEY-----'));

$source='BcIBgKb3NjvQ6YsmzpE5vTZnwGJnAyLZGDFei254aHRjvOyFgVakNj7R9RAOnKaLUWaw6cRGRyGWOQuGU3wZHBIO36vpQvEh+rLZszIKO2zb5NSKXYnQIzt4LJn2aEK3fXwk7ADXSxAeBC+pB+59ADxBvBbehmBK7t2W7pPPCro=';

openssl_private_decrypt(base64_decode($source),$newsource,$privateKey);
echo "String decrypt : $newsource";

1 个答案:

答案 0 :(得分:0)

您需要删除第一行中的base64_encode函数。

$privateKey = openssl_get_privatekey ('----- BEGIN RSA PRIVATE KEY -----
...
...
...
----- END RSA PRIVATE KEY ----- ');