This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Asymmetric Encryption In PHP – DEV Community


Hey there, uneven encryption is superior, proper? It is the core encryption idea for all end-to-end encrypted chat programs like WhatsApp, sign, wire, and many others. However how will we do this uneven encryption in PHP? Nicely, earlier than we begin doing uneven encryption, let’s perceive the core idea behind the system.
As an example, Alice, wish to ship a message to Bob, that no center man however can learn. To do this, Alice must entry Bob’s public key, encrypt her message utilizing Bob’s personal key and ship the encrypted message to bob once more.
However wait, if Alice can get Bob’s encryption key, would not that imply, anybody can Bob’s public encryption key? Nicely, sure, anybody can. However here is the difficult half, even when somebody can encrypt a message for Bob utilizing his public key, it doesn’t suggest, anybody can decrypt and skim Bob’s message utilizing his Public key. As a result of the general public key can solely encrypt messages. Decryption takes the personal key, which solely Bob is aware of and he would not share his personal key with anybody! And that is the entire idea of Uneven Encryption. Sufficient of talks, time to put in writing some code.
Oh, Did I point out I wrote a whole article on Encryption in PHP?
We’ll be utilizing the OpenSSL PHP extension for this. So, be sure to have a legitimate openssl.cnf put in in your system. If you do not have this in your gadget, do not panic, right here is the installation process
Now here is the code block for a whole uneven encryption course of on PHP utilizing the OpenSSL extension. When you’ve got any doubt or confusion concerning the code block beneath, be sure to try my full article I discussed above ;p

<?php 
//Preliminary
$res = openssl_pkey_new(array("private_key_bits" => 4096));
//Export the personal key 
openssl_pkey_export($outcome, $privKey);
//Get the general public key
$publicKey = openssl_pkey_get_details($outcome)['key'];
//Alice's Secret Message
$plaintext = "Nicely, I am from Blogdesire ;p";
//Export the encrypted message to $encrypted
openssl_public_encrypt($plaintext,$encrypted,$publicKey);
//Print encrypted message
echo $encrypted;
//Time to decrypt the message utilizing the personal key
openssl_private_decrypt($encrypted, $original_message, $privKey);
//Print the decrypted message
echo $original_message;
Enter fullscreen mode

Exit fullscreen mode

Hope you discovered this useful.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?