im using linux ubuntu server with nginx,mysql,php7.3
<?php
//session_start();
$rsa = new Crypt_RSA();
extract($rsa->createKey());
$rsa->loadKey($privatekey);
$_SESSION['pr']=(empty($_SESSION['pr'])) ? $rsa : $_SESSION['pr'];
$rsa->loadKey($publickey);
$_SESSION['pu']=(empty($_SESSION['pu'])) ? $rsa : $_SESSION['pu'];
?>
<br /><input type="button" value="Show Encrypted" onclick="alert(crypted);"/> <input type="button" value="Show Decrypted" onclick="alert(decrypt);"/><br />
<script type="text/javascript">
var crypt = new JSEncrypt();
var public_key = '<?php echo der2pem($_SESSION["pu"], "RSA PUBLIC KEY";?>';
crypt.setPublicKey(public_key);
var input='i\'ve got a power';
var crypted=crypt.encrypt(input);
var decrypt=crypt.decrypt(crypted);
alert(crypted);
alert(decrypt);
</script>
<?php
// here i put the crypted var showed in alert
$var=base64_decode('##put your variable here');
$rsa->loadKey($_SESSION['pr']);
$dec=$rsa->decrypt($var);
echo '<br />'.$dec;
?>
Hey, neubert! Thanks for the answer you really helped me to figure out the exact issue that i've got: when i use crypt.encrypt function without setting a key first it creates a key pair itself, and then setting it itself, so i will not be able to decrypt the message using keys saved in my php session. But the next problem was the pem rsa format that crypt.setKey function is requiring. So now i'd like to ask do you know how to convert b64 encoded $rsa into pem format, i have tried to add corresponding lines on both sides of key but it doesn't help. Should i explode it to delimit into 64 bytes lines with "\n" at the end of each one? Such a function has been found here
Try adding this before you perform the actual decryption operation in phpseclib:
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
phpseclib uses the more secure but less common OAEP padding, by default.