我正在开发一个存储公共/私有RSA加密内容的网站,直到最近我一直在通过javascript完成这一切。我现在需要能够使用PHP中的公钥加密,我已经失去了最后6个小时来解决这个问题。如果需要,我愿意更换图书馆,但目前我正在尝试这个:
https://www.pidder.de/pidcrypt/?page=demo_rsa-encryption
键:
$pub_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVd/gb2ORdLI7nTRHJR8C5EHs4
RkRBcQuQdHkZ6eq0xnV2f0hkWC8h0mYH/bmelb5ribwulMwzFkuktXoufqzoft6Q
6jLQRnkNJGRP6yA4bXqXfKYj1yeMusIPyIb3CTJT/gfZ40oli6szwu4DoFs66IZp
JLv4qxU9hqu6NtJ+8QIDAQAB
-----END PUBLIC KEY-----";
$priv_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDVd/gb2ORdLI7nTRHJR8C5EHs4RkRBcQuQdHkZ6eq0xnV2f0hk
WC8h0mYH/bmelb5ribwulMwzFkuktXoufqzoft6Q6jLQRnkNJGRP6yA4bXqXfKYj
1yeMusIPyIb3CTJT/gfZ40oli6szwu4DoFs66IZpJLv4qxU9hqu6NtJ+8QIDAQAB
AoGADbnXFENP+8W/spO8Dws0EzJCGg46mVKhgbpbhxUJaHJSXzoz92/MKAqVUPI5
mz7ZraR/mycqMia+2mpo3tB6YaKiOpjf9J6j+VGGO5sfRY/5VNGVEQ+JLiV0pUmM
doq8n2ZhKdSd5hZ4ulb4MFygzV4bmH29aIMvogMqx2Gkp3kCQQDx0UvBoNByr5hO
Rl0WmDiDMdWa9IkKD+EkUItR1XjpsfEQcwXet/3QlAqYf+FE/LBcnA79NdBGxoyJ
XS+O/p4rAkEA4f0JMSnIgjl7Tm3TpNmbHb7tsAHggWIrPstCuHCbNclmROfMvcDE
r560i1rbOtuvq5F/3BQs+QOnOIz1jLslUwJAbyEGNZfX87yqu94uTYHrBq/SQIH8
sHkXuH6jaBo4lP1HkY2qtu3LYR2HuQmb1v5hdk3pvYgLjVsVntMKVibBPQJBAKd2
Dj20LLTzS4BOuirKZbuhJBjtCyRVTp51mLd8Gke9Ol+NNZbXJejNvhQV+6ad7ItC
gnDfMoRERMIPElZ6x6kCQQCP45DVojZduLRuhJtzBkQXJ4pCsGC8mrHXF3M+hJV+
+LAYJbXrQa4mre59wR0skgb6CwGg1siMrDzJgu3lmBB0
-----END RSA PRIVATE KEY-----";
/*
使用phpseclib的PHP代码:“
$rsa = new Crypt_RSA();
//extract($rsa->createKey());
$plaintext = 'eKFqZhGXg/QzTKI9dbvamIxDSltVWoz73DSowr87ipWHRSqKBAE463VCrcNcDKyW
gleCanPtV4NQ0qEImhf2xpIHFPeaCc++a0u7ZhZF8vpn5E8AGz97lqs3o7XGwmm1
EUlCeHh3c6574wiUd93eWBWLhxQUJPK66V3CQT0SrEQ=
';
$plaintext=base64_decode($plaintext);
$rsa->loadKey($priv_key);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->decrypt($plaintext);
echo $ciphertext;
只是回显空文本,明文变量是使用这些密钥通过pidcrypt网站加密的数据。为什么这不起作用?我也试过通过openssl运行它:
$res = openssl_get_privatekey($priv_key);
/*
* NOTE: Here you use the returned resource value
*/
openssl_private_decrypt($plaintext,$newsource,$res);
echo "String decrypt : $newsource";
也没用。我已阅读并且everythibng说pidcrypt是openssl兼容的。我似乎无法弄明白。
答案 0 :(得分:0)
这对我有用:
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDVd/gb2ORdLI7nTRHJR8C5EHs4RkRBcQuQdHkZ6eq0xnV2f0hk
WC8h0mYH/bmelb5ribwulMwzFkuktXoufqzoft6Q6jLQRnkNJGRP6yA4bXqXfKYj
1yeMusIPyIb3CTJT/gfZ40oli6szwu4DoFs66IZpJLv4qxU9hqu6NtJ+8QIDAQAB
AoGADbnXFENP+8W/spO8Dws0EzJCGg46mVKhgbpbhxUJaHJSXzoz92/MKAqVUPI5
mz7ZraR/mycqMia+2mpo3tB6YaKiOpjf9J6j+VGGO5sfRY/5VNGVEQ+JLiV0pUmM
doq8n2ZhKdSd5hZ4ulb4MFygzV4bmH29aIMvogMqx2Gkp3kCQQDx0UvBoNByr5hO
Rl0WmDiDMdWa9IkKD+EkUItR1XjpsfEQcwXet/3QlAqYf+FE/LBcnA79NdBGxoyJ
XS+O/p4rAkEA4f0JMSnIgjl7Tm3TpNmbHb7tsAHggWIrPstCuHCbNclmROfMvcDE
r560i1rbOtuvq5F/3BQs+QOnOIz1jLslUwJAbyEGNZfX87yqu94uTYHrBq/SQIH8
sHkXuH6jaBo4lP1HkY2qtu3LYR2HuQmb1v5hdk3pvYgLjVsVntMKVibBPQJBAKd2
Dj20LLTzS4BOuirKZbuhJBjtCyRVTp51mLd8Gke9Ol+NNZbXJejNvhQV+6ad7ItC
gnDfMoRERMIPElZ6x6kCQQCP45DVojZduLRuhJtzBkQXJ4pCsGC8mrHXF3M+hJV+
+LAYJbXrQa4mre59wR0skgb6CwGg1siMrDzJgu3lmBB0
-----END RSA PRIVATE KEY-----');
$ciphertext = 'B0xBiIroAo7xpHuDThpsAIAAlmlJtK1M0I4wGSJQuRMj5vy0g/+QeDYA4v+9Pl5m
R/eiXzmbNF/WrBNJkgTJQalXK8zLGXFs1YxSnpVazBIAZo+zrnwy6g0eZ4U6exEx
tVcU/ay+oRa+K0Rn03N29y3wi5Dy46hTSLQW12a7zLY=';
$ciphertext = 'wYevij6cVGuf6+675lL81dK4oQxxINn0ESWOIKDe76u9iAdzg5JwJGuiealOAKDY
GQPCzWFtY4i+xpC3lbxc01tuzwLqLDyc78d5ejmEMraPdToaX+Z7+naiabXUUQlg
PSxsVlpL9b5S6/kB9BVJK9aOYMBlonJEKs9IZKKuoVw=';
$ciphertext = str_replace(array("\r","\n",' '), '', $ciphertext);
$ciphertext = base64_decode($ciphertext);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo base64_decode($rsa->decrypt($ciphertext));
这是在https://www.pidder.de/pidcrypt/?page=demo_rsa-encryption使用默认的公钥/私钥对。密文来自该页面。
我猜你做错了什么:你是从“加密文本”文本框中复制/粘贴文本,但是没有删除换行符。