How to Encrypt Large Messages with Asymmetric Keys and phpseclib



Originally published at:

This tutorial will show you how to encrypt arbitrarily large messages with asymmetric keys and a PHP library called phpseclib.

Keys on a sheet with encrypted data


Most of us understand the need to encrypt sensitive data before transmitting it. Encryption is the process of translating plaintext (i.e. normal data) into ciphertext (i.e. secret data). During encryption, plaintext information is translated to ciphertext using a key and an algorithm. To read the data, the ciphertext must be decrypted (i.e. translated back to plaintext) using a key and an algorithm.

Continue reading this article on SitePoint


I really like using phpseclib for SFTP, and it was cool to read about this. Thanks for sharing.


You are certainly welcome! I've enjoyed using it, and leaning about it. I was thrilled to be able to share.


I also used it. Actually, at first I was tried online php guide to learn php online and then I used this. This is really very helpful.

echo '<pre>';
echo "Starting Plaintext:\n$plaintext";
echo "\n --- \n";
$ciphertext = encrypt_message($plaintext,$public_key);
echo "\nCiphertext:\n";
echo chunk_split($ciphertext);
$plaintext2 = decrypt_message($ciphertext,$private_key);
echo "\n\nDecrypted Back into Plaintext\n\n";

echo $plaintext;

What does it mean on test.php file when you print $plaintext beofre and after it seems blackmail to reader. It is failed to decrypt original text because when I print $plaintext2 I got a garbage output like cipher text.

However, please correct and run yourself then edit code again.


I found the same problem. The code in github is buggy, and different to what's in the article. The functions in the text of the article above have the bugs fixed. So, use those instead.

closed #7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.