Javascript pidCrypt RSA and PHP RSA decryption

665 views Asked by At

i'm working on a site that invovles storing public/private RSA encrypted content, until recently i've been doing it all via javascript. I now need to be able to encrypt with public key in PHP, i've lost the last 6 hours to this problem. I'm willing to change libraries if needbe, but currently im trying this:

https://www.pidder.de/pidcrypt/?page=demo_rsa-encryption

keys:

$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-----";
/*

PHP code using phpseclib:"

$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;

just echoing empty text, the plaintext variable is data encrypted via the pidcrypt website using those keys. why isnt this working? i've also tried running it through 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";

also did not work. i've read and everythibng says pidcrypt is openssl compatible. i just cant seem to figure it out.

1

There are 1 answers

0
neubert On

This works for me:

<?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));

That's using the default public / private key pair at https://www.pidder.de/pidcrypt/?page=demo_rsa-encryption . The ciphertext came from that page as well.

My guess as to what you're doing wrong: you're copy / pasting the text from the "Encrypted text" textbox but aren't removing the new line characters.