RSA iOS encrypt & PHP decrypt - php

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='));

Related

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/gb2ORdLI7nTRHJR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CzWFtY4i+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.

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

Trouble with RSA using phpseclib in 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-----";

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
S5YEvVSWVpu3D7x3IO2XVtTHlWTTRZk2c+ZuFR3eIwDZbmuOiTmuJMQCJiRd8pqf
4sVCoKzWrUmBuT2pc/sdUQ4IPylGK3yGVyxHNa8gCkBs/zVq3I0Zu7Nx8mW8DTn9
EvP02mkFHynJZgKE0SJftu6Mp9BDl4qR4T8896K+Z1Fi3qx3iK6nkjYjXCYYZB90
6uWiqm7BK/4zbzuc604+BZ4LOPgCsO+aZjx3ywIDAQABAoIBABgTfp5Q4/UkCX2D
BFVG9/+r5trz1HWNQvZGyKseaaWySX+DtGd/OX3o5DED7joZxCibpg3BcbmQ7EU3
RfxlsVjC2DxgkevsSWPcvpdZZ/UcojeMRBPFC6KLeaqymIVtWI3wMjOtkQLJXDCP
UmY0LM6COzGHbxHi+8B44brcPRwe+zJK5ALhPfFEyPsnz9Rw2PLBWceXNeZYN0ja
C699r+a/t0kdJ0+49LLTvFjFECZLArZTK1GZbzACeM/fFl7XvK3tsdAkizys0Dwl
eyDtINkWlg+hzt8uijVY78MvP36RM8k4QIjSX/2p/jupsyZD4I2R9OW+ml9sImok
Ud5YKaECgYEA7xbLLvZ5ddpf/yBSVpGytfU/PzWK5L9cPRbrQPll6iKgimAOTULi
VQAQYaCDL/er/zjVU+a2Kxv1G8yX/LXHXtpSbxIVvgTqtAuGYT0hGkCFtUQaviJG
z/r3DnGiF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F+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:6c:d6:52:25:
49:f9:ae:fa:1b:b1:67:9f:f2:2e:e6:6d:01:6c:bd:
39:be:52:88:05:de:11:b3:d3:a9:80:36:e2:a9:fc:
00:26:0b:fc:0a:f6:0a:c9:18:db:61:d1:27:3f:e9:
a1:0d:45:b8:c4:63:7f:ef:ea:b1:13:f4:54:5d:9a:
a5:2e:32:9f:bb:bf:88:a0:92:21:18:5e:15:8f:30:
fe:ef:20:cf:1c:7f:ad:29:2c:ff:11:26:a0:10:a0:
3a:af:27:82:e4:24:a9:ad:61
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5NZelJZ5QRiRfoN9tcb5zYrwvVWx1SR6Zo5moS
S5YEvVSWVpu3D7x3IO2XVtTHlWTTRZk2c+ZuFR3eIwDZbmuOiTmuJMQCJiRd8pqf
4sVCoKzWrUmBuT2pc/sdUQ4IPylGK3yGVyxHNa8gCkBs/zVq3I0Zu7Nx8mW8DTn9
EvP02mkFHynJZgKE0SJftu6Mp9BDl4qR4T8896K+Z1Fi3qx3iK6nkjYjXCYYZB90
6uWiqm7BK/4zbzuc604+BZ4LOPgCsO+aZjx3ywIDAQABAoIBABgTfp5Q4/UkCX2D
BFVG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/yLuZt
AWy9Ob5SiAXeEbPTqYA24qn8ACYL/Ar2CskY22HRJz/poQ1FuMRjf+/qsRP0VF2a
pS4yn7u/iKCSIRheFY8w/u8gzxx/rSks/xEmoBCgOq8nguQkqa1h
-----END RSA PRIVATE KEY-----

A good example using RSA in php

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
/Vqj13uvcjadLctC2yp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vuVZAw0+x0tvrJEL
n2A53+xM/Z1mGd7UHl8CgYARx9UE0uaW9XDvL2R83/EzbJZ2bmBCgTmpa7W10irx
P0hPIQRSOEAkoWNGaebX9zbM6En3K2ly3tgyg8eEMkSu0zg4Ck5g2XvQzYnfpleS
ShT+cXeG+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.

Categories