Trouble with RSA using phpseclib in PHP - php

I'm trying to use the RSA implementation in phpseclib, I thought it would be easier to do the code once in a function and re-use the function.
When I've tried texting the code I get a error saying "decryption error"
Testing also made me realise that the ciphertext was different every time the code ran, so I'm clearly doing something wrong there!
The code for the functions is:
require_once "Crypt/RSA.php";
require_once "Crypt/AES.php";
//Function for encrypting with RSA
function rsa_encrypt($string, $public_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($public_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the encrypted version
return base64_encode($cipher->encrypt($string));
}
//Function for decrypting with RSA
function rsa_decrypt($string, $private_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($private_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the decrypted version
return $cipher->decrypt($string);
}
I've tried to test it using the following:
(The keys are just for testing so that's why its hardcoded).
It is in here that everytime the code is run that the value of $ciphertext changes everytime
//Private key
$private_key = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HKU0jQjX/n/db9vz/x0i3te/bK LNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJAHmbrNtrUB6ckrYLF75Y50nUN sBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQABAoGAGsiMtAyFu23ac0PdvOuhqm3O/vXAF0Ki zxwBVURfxM6LfiOOcRPe5RSKGTW+Cl7AQVEmMlsa/LtBhLhQ9LNQ5L/4oTmRhCGiZZEmccAdjKsx yMeaxkp+ZHvMxMKQNDgYg1CXqrCrpwwUuMUlA26tfxZ3xSFtFyDTaV9mgDQ1IGECQQCkX9Tum7D1 vQTwbhbYnu7eC4eUOaZeGWSEs2csK7U6vfZ3BzUZW/0tPqcSpQqcNxMtY9TiUsNRj1uM6jX3byp7 AkEA6Ab+wvOTNRtES77OAYG9gHGZZ+iXjQJ/6Z7JehN4p41UbDIf9nNUOLUPL9z5V1uOYnl1CWoo Cw95cdhKXxEAqQJBAIU5Or6tp250ZdVslM27ewSyuY9UblfkIsk/EscFIdzbbDAqwkmsefW6yvTc mU3lgYCPYlKRG8c19tCuX1ENY5MCQAz37x9YW975Ai01ofAFn2DheJCNOINCI4IcROiU1AaRaKmP d6fftFJjFFE5iZovXNr2LOt0yn4rxD7vtuBvY9kCQGyty6YCB6qaD7qXPMhLrLbGajAIWd6ETgxv frK/BJu+buPfDky/g1FhI5R9iMtL1xH0JYLJlaVocU+xSeA9DkY= -----END RSA PRIVATE KEY-----";
//Public key
$public_key = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB -----END PUBLIC KEY-----";
//Test out the rsa encryption functions
$plaintext = "This is some plaintext to encrypt";
$ciphertext = rsa_encrypt($plaintext, $public_key);
$decipheredtext = rsa_decrypt($ciphertext, $private_key);
//Echo out results
echo sprintf("<h4>Plaintext for RSA encryption:</h4><p>%s</p><h4>After encryption:</h4><p>%s</p><h4>After decryption:</h4><p>%s</p>", $plaintext, $ciphertext, $decipheredtext);
EDIT:
Sample output is:
Plaintext for RSA encryption:
This is some plaintext we will encrypt
After encryption:
‘˜!ˆ_枦WýF¦E×9&ùš!´jéÓb÷á劀É7J+۪߯׎È㩨ɣ#(÷)ÃX„)÷O‘­Ë±N#Øv«ÓÌPƒ¹—Âî!a¢¦a&Á½Á˜ö‰ºŠCðJ«vW{uAåoOÂXäÞ#÷ï`agÏ:OŒ
After decryption:
//Nothing is returned, it is blank here

I think GigaWatt's answer is the correct one. As for this:
Testing also made me realise that the ciphertext was different every
time the code ran, so I'm clearly doing something wrong there!
PKCS#1 padding adds random bytes so the ciphertext's will always be different. The PKCS#1 algorithm however knows when the plaintext ends and the random bytes begin so it's able to separate the two and return you the result you're wanting.
EDIT: I got it to work after I replaced the spaces in your keys with new lines. eg.
$private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HKU0jQjX/n/db9vz/x0i3te/bK
LNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJAHmbrNtrUB6ckrYLF75Y50nUN
sBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQABAoGAGsiMtAyFu23ac0PdvOuhqm3O/vXAF0Ki
zxwBVURfxM6LfiOOcRPe5RSKGTW+Cl7AQVEmMlsa/LtBhLhQ9LNQ5L/4oTmRhCGiZZEmccAdjKsx
yMeaxkp+ZHvMxMKQNDgYg1CXqrCrpwwUuMUlA26tfxZ3xSFtFyDTaV9mgDQ1IGECQQCkX9Tum7D1
vQTwbhbYnu7eC4eUOaZeGWSEs2csK7U6vfZ3BzUZW/0tPqcSpQqcNxMtY9TiUsNRj1uM6jX3byp7
AkEA6Ab+wvOTNRtES77OAYG9gHGZZ+iXjQJ/6Z7JehN4p41UbDIf9nNUOLUPL9z5V1uOYnl1CWoo
Cw95cdhKXxEAqQJBAIU5Or6tp250ZdVslM27ewSyuY9UblfkIsk/EscFIdzbbDAqwkmsefW6yvTc
mU3lgYCPYlKRG8c19tCuX1ENY5MCQAz37x9YW975Ai01ofAFn2DheJCNOINCI4IcROiU1AaRaKmP
d6fftFJjFFE5iZovXNr2LOt0yn4rxD7vtuBvY9kCQGyty6YCB6qaD7qXPMhLrLbGajAIWd6ETgxv
frK/BJu+buPfDky/g1FhI5R9iMtL1xH0JYLJlaVocU+xSeA9DkY=
-----END RSA PRIVATE KEY-----";
//Public key
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK
U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA
HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB
-----END PUBLIC KEY-----";

Related

Can't decrypt any message with openssl_private_decrypt

I try to decrypt RSA encrypted message from php with openssl_private_decrypt function. But the function always return null.
My function is the following :
function (){
$encrypted = 'gu6F7PFj2t+Mchp2Vi8dpiEXXVDkrjrSoOnRlCWeZYjOL6WdjvxULyEG5j9dZPORNMYx5eB9gTdni4NYYCKW3GTGS0zaKGgadChy/KZAUo40sBVmTFN3YOT6FfEr4oXZ9c0DqSRJ/LrZsZvsiHh52LleA9sapOgsm0bsgWzgnnE=';
$privateKey = '-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCMeu1UGlPod+yC/cD+qDWRyotkKly7A+gD+twgekiTguZUJPJl
ZnB2JpL/cU9oO9nOKn6WJ4DGy7qNjuOj7sLvIkgekPjW6RA2LgOgmS9gSKeOb1OI
i59QdoyNv6xOblPz7ihsc5g/KsKEVCLeP5RfBXRXEzS0KHbBnQ3zS8PktwIDAQAB
AoGASZJ7xyc+uKpOCNCDChvoamFsnVn+VMeX9LWmIbBJYFAW9wyeknKTBZ3wMK4U
S1/FE094GoW30OWT311tZUa/ehtKA0M9ODCk/Sx9eWxq/oR1e6ypJS8zzKG47DD3
3+PxKGh+IPJXo8qINvxj7yYbjMxydo8xvBufuBgjoaU9r7ECQQDcsOZIuhvhyWkf
fJFjmyFYPNJD8eJBQZnxk9uIH/yDl5vJ+bf7fNFc3t+Epnjqfgf98W5frLUeYuzT
reusJHF5AkEAovS9EowcIeMp3Ce1utSdGZpjAkGdwVwnxhDLnyO2/TuKNpYODOS7
/bLS3NCg9Kw6UF1VLltFwjsBcSoyDmMrrwJAQOMOQlHeAsvaSbSxdqzU6AGx/OIw
mOk13w6PHH3zG6nG12LB0oKHeBvbckAUCzYVGXgT8LNx6UZCxZY8Vbe9WQJAZbTq
7VCVnnDyB36hZmKdSwWZFiduFuk1yeFhtYnNBHDM6xTs4A9DZizefwOX8h4sp0wZ
hj1hZMD/PWpgo6ox8QJAFYMky0FT8n19mNRN9aGkQ2GDqO+F8jLEnFJMPXJMGyCg
85CGeRH9sF69skcNToZMR304Ms/xyfLeG72SksHT7g==
-----END RSA PRIVATE KEY-----';
return openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privateKey);
}
The decrypted is the following : Salut les amis
You can slightly modify your code to detect the error
function (){
$encrypted = 'gu6F7PFj2t+Mchp2Vi8dpiEXXVDkrjrSoOnRlCWeZYjOL6WdjvxULyEG5j9dZPORNMYx5eB9gTdni4NYYCKW3GTGS0zaKGgadChy/KZAUo40sBVmTFN3YOT6FfEr4oXZ9c0DqSRJ/LrZsZvsiHh52LleA9sapOgsm0bsgWzgnnE=';
$privateKey = '-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCMeu1UGlPod+yC/cD+qDWRyotkKly7A+gD+twgekiTguZUJPJl
ZnB2JpL/cU9oO9nOKn6WJ4DGy7qNjuOj7sLvIkgekPjW6RA2LgOgmS9gSKeOb1OI
i59QdoyNv6xOblPz7ihsc5g/KsKEVCLeP5RfBXRXEzS0KHbBnQ3zS8PktwIDAQAB
AoGASZJ7xyc+uKpOCNCDChvoamFsnVn+VMeX9LWmIbBJYFAW9wyeknKTBZ3wMK4U
S1/FE094GoW30OWT311tZUa/ehtKA0M9ODCk/Sx9eWxq/oR1e6ypJS8zzKG47DD3
3+PxKGh+IPJXo8qINvxj7yYbjMxydo8xvBufuBgjoaU9r7ECQQDcsOZIuhvhyWkf
fJFjmyFYPNJD8eJBQZnxk9uIH/yDl5vJ+bf7fNFc3t+Epnjqfgf98W5frLUeYuzT
reusJHF5AkEAovS9EowcIeMp3Ce1utSdGZpjAkGdwVwnxhDLnyO2/TuKNpYODOS7
/bLS3NCg9Kw6UF1VLltFwjsBcSoyDmMrrwJAQOMOQlHeAsvaSbSxdqzU6AGx/OIw
mOk13w6PHH3zG6nG12LB0oKHeBvbckAUCzYVGXgT8LNx6UZCxZY8Vbe9WQJAZbTq
7VCVnnDyB36hZmKdSwWZFiduFuk1yeFhtYnNBHDM6xTs4A9DZizefwOX8h4sp0wZ
hj1hZMD/PWpgo6ox8QJAFYMky0FT8n19mNRN9aGkQ2GDqO+F8jLEnFJMPXJMGyCg
85CGeRH9sF69skcNToZMR304Ms/xyfLeG72SksHT7g==
-----END RSA PRIVATE KEY-----';
if (!openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privateKey)) {
throw new Exception(openssl_error_string());
}
return $decrypted;
}
The result on my machine is:
PHP Fatal error: Uncaught Exception: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error in /home/foobar/test.php:20
So, there is a "pkcs decoding error" when decoding your encrypted string. And your openssl_private_decrypt failed to decrypt your message. Please check the way you encrypted / base64_encode the text to see if you have any mistake there.

RSA Decryption with phpseclib is failing

I have mobile application that encrypts strings with RSA and sends that encrypted data to PHP web server.
After a search I found that phpseclib is used to decrypt RSA
Latest version 2.0.
No matter what I do I receive despite error despite used mode
Am i doing something wrong?
What I have tried:
Private key for decryption:
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAJyHUgC1ijhsETeuoNMh4c4yrFoL4juL/yDderMC9fBd1TFgEoJ5dxzMzdlzVVj7Vc/H/I+k13yY3W0MknS//k8CAwEAAQJAaaL1l57s8lkUYZTL2tFh9+vA32BnxLIdc0ullAwqeJV21wXcEyDA67fbmBywdt+pVKkeO2NU7fD3e+DZREuJ0QIhAPZNo9jirkRl4i/Lv3jWt6SmeUBeyIKK0u4lZiBF9KAZAiEAorDjj2c9WBdP46S9hK7yj0U5/0QHB0pO01j9QSVBvqcCIQDrYre7hqdU5qmLVATgzxMiX5ZxViP53gJHZaZ8IV7vwQIgTTYEGafWjjsqGBC0PQdGaMZi+wnPCB+0/0rpjoRfClsCIBPzZw+lappnVxXHuUoQQeN6uevqSvmgvC42UyA4HABa
-----END RSA PRIVATE KEY-----
Encrypted message (base64 encoded):
SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n
PHP sample code:
$rsa = new RSA();
$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
$rsa->loadKey($privateKey);
echo $rsa->decrypt(base64_decode($strBase64));
Result:
Notice: Decryption error in C:\xampp\htdocs\webservice\vendor\phpseclib\phpseclib\phpseclib\Crypt\RSA.php on line 2553
Mode:
$rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP);
Result:
Notice: Decryption error in C:\xampp\htdocs\webservice\vendor\phpseclib\phpseclib\phpseclib\Crypt\RSA.php on line 2432
Mode:
$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE);
Result:
string(128) "I�W��B'q����;k��}�1������=��x���*���_��aq�)�D� '�m{��� ��n���C:��t �E����R=�S�y�3$QC�EV.3C�{�.Y�jx�6��!�e�˱]�I ����S�/�'I�|"
As James J Polk observed, your base64 encoded string has some bad characters in it. idk what you're using in PHP as your delimiter but this worked for me:
$strBase64 = 'SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n';
$strBase64 = str_replace(['\/', '\n'], ['/', ''], $strBase64);
If you're using double quotes instead of single quotes do this:
$strBase64 = "SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n";
$strBase64 = str_replace(['\/', "\n"], ['/', ''], $strBase64);
$rsa = new RSA();
$rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP);
$ciphertext = base64_decode($string);
$rsa->setMGFHash('sha1');
$rsa->setHash('sha256');
$rsa->loadKey($pkey); // public key
$output = $rsa->decrypt($ciphertext);
return $output;

Javascript pidCrypt RSA and PHP RSA decryption

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.
This works for me:
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDVd/gb2ORdLI7nTRHJR8C5EHs4RkRBcQuQdHkZ6eq0xnV2f0hk
WC8h0mYH/bmelb5ribwulMwzFkuktXoufqzoft6Q6j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/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.

Votifier PHP Encryption

I've been trying to add votifier functionality to my site but so far, without any luck at all.
My problem is that the encryption using the openssl_public_encrypt returns an empty response.
Here's my code: http://pastebin.com/wakjcxSF
I call it like this:
$vote = new Vote(USERNAME, SERVERIP, SERVERPORT, VOTIFIERKEY);
$vote->castVote();
The output of the print_r:
0
VOTE Something test 127.0.0.1 1376050280
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiRda9+W9mXLz1Mvc4hXMo rea2NPPJVgbQCypwpgIUK0Q7vQ2sK+Om2yCyTtwIvdOCAa1Uh+cX2Drs3xHkPutw/ rkTiFEWWQ3ol3jxMm+5BXUSESSK+P26nr7JvFddnOdVM1FFVCwFXYmYIwigeXK3lO qrOLFdxVEbpG5wEcNPOVIqq1RKfKQIFGvYfIVpRCaj3yMRlSK4S1jQtlqMy5Tnva+ xqaFjEHN2QJRwLecmkPnGsWicEfoc+khV64/06YKg5Becqx69sS+snY8SM1E/atZk b/eqm165A7Awzw3e9Fl2Uk4F7kUwrRGZDjzu6knr6Vr13slXDxzEKd33xv8/QIDAQ AB -----END PUBLIC KEY-----
Original public key:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiRda9+W9mXLz1Mvc4hXMorea2NPPJVgbQCypwpgIUK0Q7vQ2sK+Om2yCyTtwIvdOCAa1Uh+cX2Drs3xHkPutw/rkTiFEWWQ3ol3jxMm+5BXUSESSK+P26nr7JvFddnOdVM1FFVCwFXYmYIwigeXK3lOqrOLFdxVEbpG5wEcNPOVIqq1RKfKQIFGvYfIVpRCaj3yMRlSK4S1jQtlqMy5Tnva+xqaFjEHN2QJRwLecmkPnGsWicEfoc+khV64/06YKg5Becqx69sS+snY8SM1E/atZkb/eqm165A7Awzw3e9Fl2Uk4F7kUwrRGZDjzu6knr6Vr13slXDxzEKd33xv8/QIDAQAB
I used this tutorial: http://topg.org/php_votifier.
I tried to look around on google but the problems was invalid key problems. Result:
google (com)/search?q=Votifier+PHP+Encryption&oq=Votifier+PHP+Encryption&aqs=chrome.0.69i59j69i60j69i62l2.6301j0&sourceid=chrome&ie=UTF-8#bav=on.2,or.r_cp.r_qf.&fp=917b53cc1ec39b52&q=Votifier+PHP+Encryption
Found this too: teamavolition (com)/topic/14380-solved-php-rsa-encryptions-votifier/
Best Regards,
- ImThatPedoBear
That's sorta a "naked" RSA public key. OpenSSL only works with public keys that are in an X.509 cert.
My recommendation would be to use phpseclib's Crypt_RSA. eg.
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey('...'); // public key
$plaintext = '...';
//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$ciphertext = $rsa->encrypt($plaintext);

Decryption with openssl not working

Here is my php script
<?php
$pubfile = fopen("public_key.pem", "r");
$public = fread($pubfile, 8192);
fclose($pubfile);
openssl_get_publickey($public);
$prifile = fopen("private_key.pem", "r");
$private = fread($prifile, 8192);
fclose($prifile);
openssl_get_privatekey($private);
$plaintext = "hello";
openssl_public_encrypt ($plaintext , $crypted , $public);
openssl_private_decrypt($crypted, $decrypted, $private);
echo $decrypted;
?>
my respective public and private keys
private_key.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5NZelJZ5QRiRfoN9tcb5zYrwvVWx1SR6Zo5moS
S5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ywu4F2iec+z9IFyLib
NiUPQQUI53LBrx74+ZqG1wKBgBhFlTXLb4S3RWEkYi4ZTZ8jGWBRm82hblZ8fcOJ
YA7xCrduH90IM7mU2bpDACx/UCfjQ/qM0iXaYDV6QBlhwybk9NpNbmVUHC+eBfYL
RMIyHcAWsdNLmqjcfZjb+LcyQB+NLhus1vgWfRS3nwjGufJk1Js7t7z/LCNCXK1J
4izRAoGBANSNXXYF32hQ8CJt/LqbtQaRcffdwoytFVps1lIlSfmu+huxZ5/yLuZt
AWy9Ob5SiAXeEbPTqYA24qn8ACYL/Ar2CskY22HRJz/poQ1FuMRjf+/qsRP0VF2a
pS4yn7u/iKCSIRheFY8w/u8gzxx/rSks/xEmoBCgOq8nguQkqa1h
-----END RSA PRIVATE KEY-----
public_key.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnDI+Q/tbVToQ30DBB0y6
89ivB01gRhuAEYCqMeezhE8CrzQN3WGsox3Q3ortLZkbuFrMQdBN6ZjD1w0k9utx
Lr3MFRbXqAxeXd7NlCz9biBaQw1LboKr10kVyENPTRT2NrQnfsFsI6WfvMKiu62/
ZEHT80DMUvilSSuRbNKrfwMqhKiRbM83Olxepk8XBNyRimvypxaPhcEiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----
$crypted returns to me encrypted data however decryption echos nothing.
I did an echo of the result and it comes back as NULL any ideas?
Solved. I don't know what happened but after generating new key pairs script worked. I might have possibly mixed up my key pairs
Using openssl rsa to dump the key contents reveals that the private and public keys you posted above have a different modulus. For a matching keypair, the modulus would be identical in the private and public keys. In other words, you definately mixed up different keys there.
To detect this, OpenSSL proper has the RSA_check_key() function to check whether an RSA keypair is usable, but the PHP wrapper for OpenSSL does not seem to expose that function to the user.
Public key:
% openssl rsa -in public_key.pem -pubin -text
Public-Key: (2048 bit)
Modulus:
00:9c:32:3e:43:fb:5b:55:3a:10:df:40:c1:07:4c:
ba:f3:d8:af:07:4d:60:46:1b:80:11:80:aa:31:e7:
b3:84:4f:02:af:34:0d:dd:61:ac:a3:1d:d0:de:8a:
ed:2d:99:1b:b8:5a:cc:41:d0:4d:e9:98:c3:d7:0d:
24:f6:eb:71:2e:bd:cc:15:16:d7:a8:0c:5e:5d:de:
cd:94:2c:fd:6e:20:5a:43:0d:4b:6e:82:ab:d7:49:
15:c8:43:4f:4d:14:f6:36:b4:27:7e:c1:6c:23:a5:
9f:bc:c2:a2:bb:ad:bf:64:41:d3:f3:40:cc:52:f8:
a5:49:2b:91:6c:d2:ab:7f:03:2a:84:a8:91:6c:cf:
37:3a:5c:5e:a6:4f:17:04:dc:91:8a:6b:f2:a7:16:
8f:85:c1:22:41:81:65:8a:b5:6a:fe:3b:f2:51:a2:
60:d3:55:63:5f:1a:75:3c:aa:97:c0:8b:ab:17:e1:
2e:f9:6e:9b:e0:ea:2d:2c:76:34:10:03:dc:90:d8:
8f:e1:73:87:30:d5:9d:fc:d1:b9:8d:5d:c5:98:4c:
f3:e4:29:fd:21:d3:03:1d:27:b0:70:0e:bf:ae:c3:
aa:57:1d:1b:64:e0:67:4a:c9:95:2e:ef:5f:3e:c0:
b2:ae:ce:de:05:25:d9:4a:52:05:28:70:f7:f4:74:
7a:79
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnDI+Q/tbVToQ30DBB0y6
89ivB01gRhuAEYCqMeezhE8CrzQN3WGsox3Q3ortLZkbuFrMQdBN6ZjD1w0k9utx
Lr3MFRbXqAxeXd7NlCz9biBaQw1LboKr10kVyENPTRT2NrQnfsFsI6WfvMKiu62/
ZEHT80DMUvilSSuRbNKrfwMqhKiRbM83Olxepk8XBNyRimvypxaPhcEiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----
Private key:
% openssl rsa -in private_key.pem -text
Private-Key: (2048 bit)
modulus:
00:b7:0c:6f:3e:96:ef:6d:e2:42:ce:d5:a5:40:20:
b4:c3:69:90:4c:d2:02:6a:ba:48:91:b1:53:ce:51:
28:a1:70:3c:66:8d:f4:b0:df:5c:74:7d:08:7d:af:
e9:22:17:f1:d0:b6:e6:f9:ed:37:17:3e:4d:65:e9:
49:67:94:11:89:17:e8:37:db:5c:6f:9c:d8:af:0b:
d5:5b:1d:52:47:a6:68:e6:6a:12:4b:96:04:bd:54:
96:56:9b:b7:0f:bc:77:20:ed:97:56:d4:c7:95:64:
d3:45:99:36:73:e6:6e:15:1d:de:23:00:d9:6e:6b:
8e:89:39:ae:24:c4:02:26:24:5d:f2:9a:9f:e2:c5:
42:a0:ac:d6:ad:49:81:b9:3d:a9:73:fb:1d:51:0e:
08:3f:29:46:2b:7c:86:57:2c:47:35:af:20:0a:40:
6c:ff:35:6a:dc:8d:19:bb:b3:71:f2:65:bc:0d:39:
fd:12:f3:f4:da:69:05:1f:29:c9:66:02:84:d1:22:
5f:b6:ee:8c:a7:d0:43:97:8a:91:e1:3f:3c:f7:a2:
be:67:51:62:de:ac:77:88:ae:a7:92:36:23:5c:26:
18:64:1f:74:ea:e5:a2:aa:6e:c1:2b:fe:33:6f:3b:
9c:eb:4e:3e:05:9e:0b:38:f8:02:b0:ef:9a:66:3c:
77:cb
publicExponent: 65537 (0x10001)
privateExponent:
18:13:7e:9e:50:e3:f5:24:09:7d:83:04:55:46:f7:
ff:ab:e6:da:f3:d4:75:8d:42:f6:46:c8:ab:1e:69:
a5:b2:49:7f:83:b4:67:7f:39:7d:e8:e4:31:03:ee:
3a:19:c4:28:9b:a6:0d:c1:71:b9:90:ec:45:37:45:
fc:65:b1:58:c2:d8:3c:60:91:eb:ec:49:63:dc:be:
97:59:67:f5:1c:a2:37:8c:44:13:c5:0b:a2:8b:79:
aa:b2:98:85:6d:58:8d:f0:32:33:ad:91:02:c9:5c:
30:8f:52:66:34:2c:ce:82:3b:31:87:6f:11:e2:fb:
c0:78:e1:ba:dc:3d:1c:1e:fb:32:4a:e4:02:e1:3d:
f1:44:c8:fb:27:cf:d4:70:d8:f2:c1:59:c7:97:35:
e6:58:37:48:da:0b:af:7d:af:e6:bf:b7:49:1d:27:
4f:b8:f4:b2:d3:bc:58:c5:10:26:4b:02:b6:53:2b:
51:99:6f:30:02:78:cf:df:16:5e:d7:bc:ad:ed:b1:
d0:24:8b:3c:ac:d0:3c:25:7b:20:ed:20:d9:16:96:
0f:a1:ce:df:2e:8a:35:58:ef:c3:2f:3f:7e:91:33:
c9:38:40:88:d2:5f:fd:a9:fe:3b:a9:b3:26:43:e0:
8d:91:f4:e5:be:9a:5f:6c:22:6a:24:51:de:58:29:
a1
prime1:
00:ef:16:cb:2e:f6:79:75:da:5f:ff:20:52:56:91:
b2:b5:f5:3f:3f:35:8a:e4:bf:5c:3d:16:eb:40:f9:
65:ea:22:a0:8a:60:0e:4d:42:e2:55:00:10:61:a0:
83:2f:f7:ab:ff:38:d5:53:e6:b6:2b:1b:f5:1b:cc:
97:fc:b5:c7:5e:da:52:6f:12:15:be:04:ea:b4:0b:
86:61:3d:21:1a:40:85:b5:44:1a:be:22:46:cf:fa:
f7:0e:71:a2:16:ef:a5:f0:53:f1:c3:e0:90:12:07:
9e:c8:d1:b8:f5:3c:f2:03:b6:8e:0e:cc:16:9b:7a:
3a:69:61:25:a6:d2:43:75:0f
prime2:
00:c3:fe:e9:45:09:20:2d:a2:b3:2c:d8:9d:08:d0:
cd:ab:fe:3f:f3:39:89:a0:c7:ba:19:52:c6:97:e1:
d5:1c:ac:e7:ea:93:d9:7d:cb:48:db:4d:72:68:9e:
4b:6f:23:da:60:3f:f6:d8:c9:43:95:e8:38:03:dc:
d9:20:bc:a4:4b:74:11:a3:f7:b1:dc:7f:bb:7b:16:
00:e2:a4:1c:2e:a5:c9:4f:c0:f9:a0:c9:4d:bc:58:
85:e8:d5:87:bb:b5:89:b5:79:6d:ab:4e:6f:70:15:
dd:47:2e:ac:dd:bc:b5:f9:25:f2:1f:8d:87:e8:7f:
77:d0:02:04:47:5d:fa:e9:85
exponent1:
31:7c:b9:4f:9e:2a:a3:cf:30:81:81:32:29:c2:fc:
87:97:1c:a1:3b:b6:e5:04:0a:d6:86:30:e9:80:f5:
ac:51:c0:83:03:c0:d2:13:1e:ff:d3:04:91:00:85:
46:df:6f:d5:62:f1:1c:ed:ab:15:1f:33:4b:ae:bc:
62:cd:49:58:03:be:7e:48:f3:01:22:c9:65:bc:e0:
f2:ad:91:75:7a:97:72:5e:87:5e:85:7f:bc:ba:c8:
7a:4f:e0:e9:f8:6e:93:60:fc:b0:bb:81:76:89:e7:
3e:cf:d2:05:c8:b8:9b:36:25:0f:41:05:08:e7:72:
c1:af:1e:f8:f9:9a:86:d7
exponent2:
18:45:95:35:cb:6f:84:b7:45:61:24:62:2e:19:4d:
9f:23:19:60:51:9b:cd:a1:6e:56:7c:7d:c3:89:60:
0e:f1:0a:b7:6e:1f:dd:08:33:b9:94:d9:ba:43:00:
2c:7f:50:27:e3:43:fa:8c:d2:25:da:60:35:7a:40:
19:61:c3:26:e4:f4:da:4d:6e:65:54:1c:2f:9e:05:
f6:0b:44:c2:32:1d:c0:16:b1:d3:4b:9a:a8:dc:7d:
98:db:f8:b7:32:40:1f:8d:2e:1b:ac:d6:f8:16:7d:
14:b7:9f:08:c6:b9:f2:64:d4:9b:3b:b7:bc:ff:2c:
23:42:5c:ad:49:e2:2c:d1
coefficient:
00:d4:8d:5d:76:05:df:68:50:f0:22:6d:fc:ba:9b:
b5:06:91:71:f7:dd:c2:8c:ad:15:5a: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