I'm trying to figure out a way to use RSA in php.
I've generated public and private keys with
ssh-keygen -t rsa
And now I'm trying to use those from my php code. Here's the code I'm using:
<?php
$string=$_GET["i"];
//$fp=fopen ("public.pem","r");
$pub_key='ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOjsALcTiv4eR6ClGcCrAHE7Jp5nL2BwVu46nx3eFlpe7cljQVUOQDgUa5yWaRoJE6qcYgQu1TDo6b/pcAWU1WVu+HzJY4g0aoV2VqamCnl3oyWoPz7ykvxbJbHv7/qTbV+IBNzSx4R7KSppvnPKsrbeiWROvZ4JF84leX+Iw== root#ip-10-83-11-72';
//fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
if (!$PK) {
echo "Cannot get public key";
echo $pub_key;
}
$finaltext="";
openssl_public_encrypt($string,$finaltext,$PK);
if (!empty($finaltext)) {
openssl_free_key($PK);
echo "Encryption OK!";
echo $finaltext;
echo "<br/><br/><br/>";
}else{
echo "Cannot Encrypt";
}
// STEP 2: Decription (Using Private Key)
//$fp=fopen ("private.pem","r");
$priv_key2='-----BEGIN RSA PRIVATE KEY-----
MIIEoAIBAAKCAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/
yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ
/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOj
sALcTiv4eR6ClGcCrAHE7Jp5nL2BwVu46nx3eFlpe7cljQVUOQDgUa5yWaRoJE6q
cYgQu1TDo6b/pcAWU1WVu+HzJY4g0aoV2VqamCnl3o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G+yl6ijZeNmJB+LDYhC0bpwkfbE97AtQoPW5DnLRVQI9uanJiiix1PvHr
iQKBgBj/RiXsDHvVJ28xZPUffZntrAD/TWXMHZWVMFgfsf8Ti0O7ypXqY43+Aoph
nfDH6xJFqU9azsiv5xQVY7X1h4+DuOamqdawIV5LtYod+ip0dxduQMfrqLld/p0m
E+jAaxvC4b6zqomFnZ9MB9eAzUg9i8UQkc4H3cH/bO6MKPG3
-----END RSA PRIVATE KEY-----
';//fread ($fp,8192);
//fclose($fp);
$PK2=openssl_get_privatekey($priv_key2);
$Crypted=openssl_private_decrypt($Data,$Decrypted,$PK2);
if (!$Crypted) {
$MSG.="<p class='error'>Cannot Decrypt ($CCID).</p>";
}else{
echo "Decrypted Data: " . $Decrypted;
}
At the end, the app crashes with:
Cannot get public key
Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in /home/menkaur/public_html/iathao.com/bookmarks/lcn.php on line 13
Cannot Encrypt
what am I doing wrong?
The key that openssl uses is in PEM format. To generate it use the following code:
openssl genrsa -des3 -out private.pem 1024
It will generate a file called "private.pem". Then you can generate the public key using:
openssl rsa -in private.pem -out public.pem -outform PEM -pubout
In PHP you can use these keys as follows:
<?php
$pub = file_get_contents('public.pem');
$pk = openssl_get_publickey($pub);
if(!$pk) ...
Then use your logic to do whatever you want.
Related
I used the following command to generate a CSR:
openssl req -nodes -newkey rsa:2048 -keyout mytestserver.key -out mytestserver.csr
I tried to decode the mytestserver.csr with the following PHP code:
<?php
$mydata="-----BEGIN CERTIFICATE REQUEST-----
MIICyDCCAbACAQAwgYIxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOSDEQMA4GA1UE
BxMHQ29uY29yZDESMBAGA1UEChMJTXljb21wYW55MQswCQYDVQQLEwJJVDETMBEG
A1UEAxMKbXl0ZXN0LmNvbTEeMBwGCSqGSIb3DQEJARYPdGVzdGVyQHRlc3QuY29t
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3CcljODQKIJQ9xnPFydW
E6fRphmfxtDZF46/J6jT7ctzG3Mv//8p9ene60BCDIWdWiDXvUUUc02nb/qiiaP5
lALl6XeLKOKs6ZEC/lknDfgOBSjqvLz+UzlOXjRdhBpaawaEbVw4rh6idHWIOFGT
lzuHmE8YBmJ7R+tfhmEETk5XtPRZ3RL173HH2R/f2YCh0uhMr4GqXVfHtWrkpiw7
gXjvaXFeQhslTSP7Pru8j39uYKqubwLTyJ1uoqHVVOIqmswz+WfSvn+c86VK44p0
oQQnFL1gHoUWBHSbTmU49ufOWog9oLHOo9GUnMRg9TntGL0s55rfmSTZ+qtyB8Lc
7wIDAQABoAAwDQYJKoZIhvcNAQEEBQADggEBALglJeJUBEmxTy1hE0jG4cPAaJe5
FOqaVCUDm4YeoTpLfYLgE7ZkWuqkhZdw4TR9wu4DkiYo9z94avrKzFj2Kq1JHjUX
weYAHzGxtFO2IUuz7DSGfSvfJ0mMkAH+QtaxNUV2olnrdA8T5hRzgbZT2RTCc0GV
iSyCKXj4O/yRt0lbBCdM/Lja3Yga2BrFtvcsqnaDO6kIj+x1YCpL3cpRsm+3XbvH
dE02+hB/iHC0bE4lQciPCASOiXbMWASWxofFjyRCP+Ha+np/FqoxRF6DIUu10dCS
wV2kkbQQhNsFuMzCJvYudsmG1ULUfjUTvCAgO9kdBweNg3nklzetoSWoE5k=
-----END CERTIFICATE REQUEST-----";
print ("\r\n begin");
print_r(openssl_x509_parse($mydata));
print ("\r\n end");
But it gives an empty result.
Any idea of how to use PHP's OpenSSL library to decode the RSA CSR I generated?
openssl_x509_parse() is used to "parse an X509 certificate." To parse a certificate signing request, you can use openssl_csr_get_subject() and openssl_csr_get_public_key().
To retrieve the public key details you would then use openssl_pkey_get_details():
$keyres = openssl_csr_get_public_key($mydata);
$key = openssl_pkey_get_details($keyres);
echo "$key[bits] bit public key follows:\n$key[key]\n";
Updates:
openssl_error_string() gives:
error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
I generate a pair of public key and private key using OpenSSL on my server:
$config = array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
I sent the Base64-encode modulus and exponent to my Android client. My Android client reconstruct the public key from the modulus and exponent received. My Android client then encrypt a message using such key. Lastly, my Android client sends back to the server the encrypted message expecting the server to be able to decrypt it.
I have a simple PHP script on my server to test the decryption of the encrypted message from my Android client:
$sms_message = $argv[1];
$sender_no = $argv[2];
echo "Message received was: '$sms_message' \n";
echo "sender's no was: $sender_no \n";
$parts = array();
$parts = explode(" ", $sms_message);
if (count($parts)==2) {
echo "code: $parts[0] \n";
if (strcmp($parts[0], "smscode")==0) {
echo "measurement: $parts[1] \n";
// retrieve the private key
$keyArr = array();
$keyArr = getKeys();
//
if ($keyArr) {
$privateKey = $keyArr["private"];
echo "$privateKey \n";
// use the private key to decrypt the message
echo openssl_private_decrypt(base64_decode($parts[1]), $decrypted, $privateKey); // this is supposed to return either TRUE or FALSE right?
echo $decrypted;
$decryptedMessages = "decrypted.txt";
if (!$fh = fopen($decryptedMessages, 'a')) {
echo "cannot open file $decryptedMessages";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($fh, $decrypted) === FALSE) {
echo "Cannot write to file ($decryptedMessages)";
exit;
}
//
fclose($fh);
}
} else {
echo "what received is not a measurement - $parts[1] \n";
}
} else {
echo "sms message malformed";
}
I ran the following:
php SmsReceiver.php 'smscode adDmHJDFmI8bC9KRcA7nPbTc2NU0sY7iM5jDHt3qJVq/AAyl9thUB3zVH5/9Jr+pTM4V+dift6UD8uB3nEU53thrY7nx55PsackCYzmBoKYTE4tazsyF7tRfAIawxvmR4lcSfKL2+A0N9ZetISoqqZAHI141n47Wtd52n0pE9tdLRGzXQlfeDOC3ntnbOKcIIhbyJWekLg+58uCLm2nfWPA4EveAd7t6RQPX4E20wXXQ1RgkVPCQsW+9WDdrbxav6y0VN7uKoBqA4/G8zn3Ol41OPtFFllBgl1BGUFWK3xcxxxZqodTCc3pTdAIHgJ4td+pktUjfbAwITt/RMC+IcA==' +6511111111
Message received was: 'smscode adDmHJDFmI8bC9KRcA7nPbTc2NU0sY7iM5jDHt3qJVq/AAyl9thUB3zVH5/9Jr+pTM4V+dift6UD8uB3nEU53thrY7nx55PsackCYzmBoKYTE4tazsyF7tRfAIawxvmR4lcSfKL2+A0N9ZetISoqqZAHI141n47Wtd52n0pE9tdLRGzXQlfeDOC3ntnbOKcIIhbyJWekLg+58uCLm2nfWPA4EveAd7t6RQPX4E20wXXQ1RgkVPCQsW+9WDdrbxav6y0VN7uKoBqA4/G8zn3Ol41OPtFFllBgl1BGUFWK3xcxxxZqodTCc3pTdAIHgJ4td+pktUjfbAwITt/RMC+IcA=='
sender's no was: +6511111111
code: smscode
measurement: adDmHJDFmI8bC9KRcA7nPbTc2NU0sY7iM5jDHt3qJVq/AAyl9thUB3zVH5/9Jr+pTM4V+dift6UD8uB3nEU53thrY7nx55PsackCYzmBoKYTE4tazsyF7tRfAIawxvmR4lcSfKL2+A0N9ZetISoqqZAHI141n47Wtd52n0pE9tdLRGzXQlfeDOC3ntnbOKcIIhbyJWekLg+58uCLm2nfWPA4EveAd7t6RQPX4E20wXXQ1RgkVPCQsW+9WDdrbxav6y0VN7uKoBqA4/G8zn3Ol41OPtFFllBgl1BGUFWK3xcxxxZqodTCc3pTdAIHgJ4td+pktUjfbAwITt/RMC+IcA==
private key found
public key found
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7dArHUiEXpEwi
...
F/EaVVWEZLevTJEdMpkfvQVr/08AlSLR3Nm33CrvQ1SfFygK0F6G6o1pQtnHlCKh
DK8/dT2CgsFuDbiAs4MRqQA36g==
-----END PRIVATE KEY-----
As you can see, openssl_private_decrypt() does not return anything, why?
Even if something is wrong with my encrypting/ decrypting process, at least give a false I'd expect.
Based64-encoded modulus of the public key:
u3QKx1IhF6RMIvncMADBhGqhdlSWnuuUz0dXr9NUzXJtgfPgvX/07w1IKTls6uj48eZ4J3s5me4xUzoRwIsxjk6Ondke2vGVJgzBZh3KQSml0dQoK/0a3Bc/bHwue3jroCCAaC/4lF6GQS5gB1gDQntkKBM+RaHaEqGldKHmF1T8Sg1zSLAU9IGBc+xDSCqgo2RepntB0npctBGmAYF8gdzN1PnAwgVfOLU/xi08ssQL1ppkrMncgPegaOOkyUZm4BXSyEY9ikYynLfoiQqEAFb9mU40yNM7LQusgqF0YhUgUIg+4fuQNscZJCJ6pS9UTQ64MHWCqrpXCeRAZ4rWeQ==
Based64-encoded exponent of the public key:
AQAB
Private key:
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7dArHUiEXpEwi
+dwwAMGEaqF2VJae65TPR1ev01TNcm2B8+C9f/TvDUgpOWzq6Pjx5ngnezmZ7jFT
OhHAizGOTo6d2R7a8ZUmDMFmHcpBKaXR1Cgr/RrcFz9sfC57eOugIIBoL/iUXoZB
LmAHWANCe2QoEz5FodoSoaV0oeYXVPxKDXNIsBT0gYFz7ENIKqCjZF6me0HSely0
EaYBgXyB3M3U+cDCBV84tT/GLTyyxAvWmmSsydyA96Bo46TJRmbgFdLIRj2KRjKc
t+iJCoQAVv2ZTjTI0zstC6yCoXRiFSBQiD7h+5A2xxkkInqlL1RNDrgwdYKqulcJ
5EBnitZ5AgMBAAECggEBAIoCAzWAF/EJ+yv8/MkypUbSIpG18TaLhwGcKsi+ND8V
sd3tKVca779X7fq71p6Ua0PdRDT9GglkPlhh9lPlptR4rbM0+OyE1CQxW+nMoO2c
tgJnyjOooq/LRdyfCLHK8t7vTtpmBwNlHD683+JIQA4gPjrq//vQD3eMHv5he79M
hbZVy93gI4x1FIQD8NPgJY3WX1tSojyOaSJCkR7cy3LjdsV17nNDTtJf5PhA584x
lJGjI6fX6VgiGhP8vmxkYYxFVzz2HrblMVod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eZ4J3s5me4xUzoRwIsxjk6Ondke2vGVJgzBZh3KQSml0dQoK/0a3Bc/bHwue3jroCCAaC/4lF6GQS5gB1gDQntkKBM+RaHaEqGldKHmF1T8Sg1zSLAU9IGBc+xDSCqgo2RepntB0npctBGmAYF8gdzN1PnAwgVfOLU/xi08ssQL1ppkrMncgPegaOOkyUZm4BXSyEY9ikYynLfoiQqEAFb9mU40yNM7LQusgqF0YhUgUIg+4fuQNscZJCJ6pS9UTQ64MHWCqrpXCeRAZ4rWeQ== AQAB
I/SmsReceiver(15814): message received is a key exchange message
I/SmsReceiver(15814): the recipient's public key modulus is 23663785522794809498963221782819553495813344590754203802091214078741934630870755737273483338578650343553350487999568641527155675069988138202941338180146715141856273325699348180697949807604837968252319802254132361756796150729526732643616381939360742823851037800072915016799286519177887771453765989612342846498554824903381084855033387403868553674907286294016751397407403976788809972626838594376008433688839811350345997686592001128890405964489889151586297624459113817319199310865303723716614014342885058854916172119790960266134365108047747227357851477353947042531226823494283658181608350838513970607286067323054395676281 and exponent is 65537
I/SmsReceiver(15814): successfully remembered the contact +6500000000 and its public key module u3QKx1IhF6RMIvncMADBhGqhdlSWnuuUz0dXr9NUzXJtgfPgvX/07w1IKTls6uj48eZ4J3s5me4xUzoRwIsxjk6Ondke2vGVJgzBZh3KQSml0dQoK/0a3Bc/bHwue3jroCCAaC/4lF6GQS5gB1gDQntkKBM+RaHaEqGldKHmF1T8Sg1zSLAU9IGBc+xDSCqgo2RepntB0npctBGmAYF8gdzN1PnAwgVfOLU/xi08ssQL1ppkrMncgPegaOOkyUZm4BXSyEY9ikYynLfoiQqEAFb9mU40yNM7LQusgqF0YhUgUIg+4fuQNscZJCJ6pS9UTQ64MHWCqrpXCeRAZ4rWeQ== and exponent AQAB
OpenSSL asymmetric cryptography is not suitable for encrypting large files unless you use S/MIME. In fact, this is what I tried:
Decoded the base64-encoded 'measurement:' field. The decoded message was 256 bytes in length.
Tried to decrypt this 256 bytes with the private key that you pasted using the following openssl command:
openssl rsautl -decrypt -in x.in -out plaintext -inkey private.key
But, I got an error instead:
RSA operation error
139982152128160:error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02:rsa_pk1.c:190:
139982152128160:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:616:
In fact, I tried this using PHP itself. But, instead of doing an echo of $decrypted, I used openssl_error_string and it gave me the exact above error.
Also, I wasn't able to encrypt a 256 byte data with public key as well. I got error from openssl indicating 139870762710688:error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for key size:rsa_pk1.c:151:.
All of this seems to indicate that OpenSSL's asymmetric cryptography is not meant for encrypting large data (in this case, 256 bytes is large enough). I am not sure how the Android client was able to encrypt it. Did it use OpenSSL as well?
However, there are relevant posts on how to deal with this. Take a look at:
how to encrypt a large file in openssl using public key
Encrypt with S/MIME: http://ashmek.weebly.com/1/post/2011/02/encrypt-large-files-with-a-public-key-via-openssl.html
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-----";
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-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkb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sUcCDA8DSEx7/0wSRAIVG32/VYvEc7asVHzNLrrxizUlY
A75+SPMBIsllvODyrZF1epdyXodehX+8ush6T+Dp+G6TYPywu4F2iec+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: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g+hzt8uijVY78MvP36RM8k4QIjSX/2p/jupsyZD4I2R9OW+ml9sImok
Ud5YKaECgYEA7xbLLvZ5ddpf/yBSVpGytfU/PzWK5L9cPRbrQPll6iKgimAOTULi
VQAQYaCDL/er/zjVU+a2Kxv1G8yX/LXHXtpSbxIVvgTqtAuGYT0hGkCFtUQaviJG
z/r3DnGiFu+l8FPxw+CQEgeeyNG49TzyA7aODswWm3o6aWElptJDdQ8CgYEAw/7p
RQkgLaKzLNidCNDNq/4/8zmJoMe6GVLGl+HVHKzn6pPZfctI201yaJ5LbyPaYD/2
2MlDleg4A9zZILykS3QRo/ex3H+7exYA4qQcLqXJT8D5oMlNvFiF6NWHu7WJtXlt
q05vcBXdRy6s3by1+SXyH42H6H930AIER1366YUCgYAxfLlPniqjzzCBgTIpwvyH
lxyhO7blBArWhjDpgPWsUcCDA8DSEx7/0wSRAIVG32/VYvEc7asVHzNLrrxizUlY
A75+SPMBIsllvODyrZF1epdyXodehX+8ush6T+Dp+G6TYPywu4F2iec+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-----
As referring to this link http://blog.iamzsx.me/show.html?id=155002, I generated a pair of key using
$ openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650
this command. Then I get private_key.pem & public_key.der.
I follow the implementation what I get the encrypted data is
<ceaedf05 f39b69ab 8618f859 599926e9 c18d9233 342c58e0 534b66af 8b33dda3 375d6a11 b95ad821 11549637 ce4c26dc 760169c6 04096aff 9ec48938 1c6b149f 8180f9bb 8e1337ce 2b492af2 e0464e4c 9ab7aabd 9da53caf 1b5e5277 3c708b43 cb6515ee c9a18b02 4941416c 7f1e36d3 dcd3705b 66f8a159 ab8a7a3f a9db8306>
but what I want is
dXXEEADvRDdk4QVM6TU9dyYtuHe8RVzP3KsWAoDDSQ2w+NELVPOv4mSVW1jIhEhb+pLsmOvLzGIW1/2qkp91M1UoPuhgplV2JEgP8EvcBn8EY43n7wVASGP9heRK0lYMfENP54f35+GFP68nU5zbrmX0IN6DGGGSNdSewzLb6s4=
Actually what I want is the functions below:
in iOS:
- (NSString *)encryptedString:(NSString *)plainText
in PHP:
function decrypted_string()
{
$encrypted_string = $_POST['encrypted_string'];
// implementation
return $plain_text;
}
How can I implement these functions?
I already googled for it, unfortunately, there is no solution that I want. Hopefully can get help in here. Thanks in advance.
You need to base64 encode the NSData before sending to php. See the answer for the following post:
Creating a base-64 string from NSData
On php end, you can use openssl_private_decrypt to decrypt. Reference below:
http://php.net/manual/en/function.openssl-private-decrypt.php
Sample:
<?php
$fp=fopen("/path/to/private_key.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
$res = openssl_get_privatekey($priv_key, "myPassword");
$data = $_POST['encrypted_string'];
if (openssl_private_decrypt(base64_decode($data), $decrypted, $res))
echo "Decrypted:$decrypted";
?>
Expanding on neubert's answer..
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->setPassword('abc123');
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,927035AD39FA7CED
un9ayDNHNdXKtAzmGB0eCxJ+3z54qNTH2nejrvxVANqDHbOh5642hIWVxrNwD0LC
dbosArWk00bAi9HGgU0UnzN4So3XM2yVxejFh98k8VuB0zPiNWHLGYhaj81BEeob
eLI3QzPCyX7CR1KJwHJC/5xHJMKN+1TCnn/nBsP12JelBJBui6R90LShfqecdJAa
oDpGGcDtPchOaHxHkv8i4AFHKm7QBDbebZZe/Bu0tKr4DJYSeQxe+a+YoqSkdRs5
JoQiqTd+kT9RT+7Kf22xtlThJQ6mh/i7rF0N32eUQsNYgs2eHmjgu55cDI6/XC+x
zIiDAsk2prtVpMzVJDkTUVnY2+AcMfgk2Oxpsx+H/9pHYWp/qma8mmRR7ALdyzce
cVmzCh4NjSPXnlrv4LCY5mEc3ObanIx6ZirmtJWwBiqADVt+aByrLicO3AoN//UU
dUiEtvIC+cl2wZ+eRRUC4uUr47a0AakkXTjZERsJt/cTNpgFdra4mVOefNxnt1rk
lfS9dQyFrBaDiS6IwpK3I9gNuvSam9SIqhSx7WT038Lh50bKvWodDck8OHvl1FwI
FiLN/VehnTJTMDe5Ee7nC6jPqVdagH0nZkNJDlVyjKCbCEKlzYOBr9mYQUqV2lSL
fbpBJOjmHesQAhP4UJNHNOhcyHBQ5WV60y0lySKPUqu20x0N3E+ByleA8g28ijwv
F79RmcldG8x5X6Ur2W7mtKbGFfLC3CFRW9r6a0ycdpfkWo/gii/tQpbBPyYPrsWe
MNkbkzdVMLOIKZLVAd+i82UrDg3zor45JhSRsRhnLRhaZuMOukcqGQ==
-----END RSA PRIVATE KEY-----');
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->decrypt(base64_decode('qieklzzWct0ab7UOows+uvlaI5VLEHwIED4+O9KjVKopGTPIVaB8ioSUNZKnh5LUepmc/gowrp4pM1X??J+e+rPlwqI98d56ijdWuDOOojGR5VCNilztPSxI6PIpQeL58y+B5EVUWv49dcTuGwuRiU/FL8APkNqdEE??k43SLKyhY7w='));
My recommndation: use phpseclib, a pure PHP RSA implementation. eg.
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->setPassword('abc123');
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,927035AD39FA7CED
un9ayDNHNdXKtAzmGB0eCxJ+3z54qNTH2nejrvxVANqDHbOh5642hIWVxrNwD0LC
dbosArWk00bAi9HGgU0UnzN4So3XM2yVxejFh98k8VuB0zPiNWHLGYhaj81BEeob
eLI3QzPCyX7CR1KJwHJC/5xHJMKN+1TCnn/nBsP12JelBJBui6R90LShfqecdJAa
oDpGGcDtPchOaHxHkv8i4AFHKm7QBDbebZZe/Bu0tKr4DJYSeQxe+a+YoqSkdRs5
JoQiqTd+kT9RT+7Kf22xtlThJQ6mh/i7rF0N32eUQsNYgs2eHmjgu55cDI6/XC+x
zIiDAsk2prtVpMzVJDkTUVnY2+AcMfgk2Oxpsx+H/9pHYWp/qma8mmRR7ALdyzce
cVmzCh4NjSPXnlrv4LCY5mEc3ObanIx6ZirmtJWwBiqADVt+aByrLicO3AoN//UU
dUiEtvIC+cl2wZ+eRRUC4uUr47a0AakkXTjZERsJt/cTNpgFdra4mVOefNxnt1rk
lfS9dQyFrBaDiS6IwpK3I9gNuvSam9SIqhSx7WT038Lh50bKvWodDck8OHvl1FwI
FiLN/VehnTJTMDe5Ee7nC6jPqVdagH0nZkNJDlVyjKCbCEKlzYOBr9mYQUqV2lSL
fbpBJOjmHesQAhP4UJNHNOhcyHBQ5WV60y0lySKPUqu20x0N3E+ByleA8g28ijwv
F79RmcldG8x5X6Ur2W7mtKbGFfLC3CFRW9r6a0ycdpfkWo/gii/tQpbBPyYPrsWe
MNkbkzdVMLOIKZLVAd+i82UrDg3zor45JhSRsRhnLRhaZuMOukcqGQ==
-----END RSA PRIVATE KEY-----');
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->decrypt(base64_decode('qieklzzWct0ab7UOows+uvlaI5VLEHwIED4+O9KjVKopGTPIVaB8ioSUNZKnh5LUepmc/gowrp4pM1X??J+e+rPlwqI98d56ijdWuDOOojGR5VCNilztPSxI6PIpQeL58y+B5EVUWv49dcTuGwuRiU/FL8APkNqdEE??k43SLKyhY7w='));