Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Symmetric encryption(AES-128-CTR) and Asymmetric encryption(public key and private key)

Reference1
Reference2
Reference3
refers

Question
1.Difference between Symmetric encryption and Asymmetric encryption in php ?

Reference1
Reference1

In Symmetric-key encryption the message is encrypted by using a key and the same key is used to decrypt the message which makes it easy to use but less secure

Asymmetric Key Encryption is based on public and private key encryption techniques. It uses two different key to encrypt and decrypt the message. It is more secure than the symmetric key encryption technique but is much slower

When do we need encryption?

We need encryption whenever we’re dealing with sensitive data. For example, while storing sensitive data into the database, or while sending sensitive data to the end-user or to another server. This makes man in the middle attacks useless. Let’s not just talk about servers, we should use encryption in our local devices. Like, our personal desktop or laptop. Our go-to storage is our personal device. Where we store everything from todo notes to banking information. So, when storing information like this on our laptop or desktop, we should use proper encryption as well. This will help you to protect that piece of data while allowing guests to use your device, or if your device ever gets stolen or even lost!

Image description
Image description

Symmetric encryption in php

Enough talks about encryption altogether 😅, let’s now talk about encryption in PHP. We will begin with symmetric encryption. The function we will use for encryption in PHP is called openssl_encrypt. This function takes 3 mandatory arguments and 5 optional arguments. The first mandatory argument is the data that you want to encrypt, the second one is the algorithm that you want to use in your encryption, and the third one is your encryption key. To see the list of available algorithms available in PHP, use the openssl_get_cipher_methods function.

Image description

If you want to learn more about the optional arguments, I will suggest you visit the URL I linked with the function name. Alright, now here’s how to do symmetric encryption in PHP.

<?php
$plaintext = "Your sensitive message";
$cipher = "AES-128-CTR";
$key = "Blog Desire";
$iv = "1232434565432123";
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);
echo $ciphertext;
Enter fullscreen mode Exit fullscreen mode

In the example above, your sensitive message is saved under the $plaintext variable, and the $cipher is the algorithm we’re using in the current encryption. The $key variable holds our encryption key, which is “Blog Desire” in our example. The next one is $iv, which holds the Initial Vector. For decryption, make sure you’ve saved your $key and $iv.

And here’s how to do asymmetric decryption in PHP

<?php
$encrypted_text = "qnHWqq1mElDqW2RCepH14VriPf5s2Q==";
$cipher = "AES-128-CTR";
$key = "Blog Desire";
$iv = "1232434565432123";
$original_text = openssl_decrypt ($encrypted_text, $cipher, $key, $options = 0, $iv);
echo $original_text;
Enter fullscreen mode Exit fullscreen mode

AES-128-CTR symmetric encryption in php

openssl_encrypt
Image description

Image description

Image description

openssl_decrypt
Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Asymmetric encryption in PHP

Image description
As we’ve discussed before, asymmetric encryption takes two keys. One is the public key and another one is the private key. The public encryption key is responsible for doing encryption and the private encryption key is required to decrypt the encrypted text. Now, our first task is to get the two keys. So, we’ll create a private and public key using the openssl_pkey_new function and save it under the $result variable. Eg:

$result = openssl_pkey_new(array("private_key_bits" => 4096));
But it’s still not ready to use. We need to export the private key out of the $result variable. Here’s how to do it.

openssl_pkey_export($result, $privateKey);
The private key is now saved under the $privateKey variable, time to get the public key and save it under the $publicKey

$publicKey = openssl_pkey_get_details($result)['key'];
Now we have both the private key and the public key. We can encrypt and decrypt data using the keys above. Let’s encrypt something using the public key

$plaintext = "Welcome To Blogdesire";
openssl_public_encrypt($plaintext,$encrypted_text,$publicKey);
We have the encrypted version of the plaintext in the $encrypted_text variable now. And we can easily decrypt the encrypted text using the private key if want.

openssl_private_decrypt($encrypted_text, $plaintext, $privateKey);
It’s just that simple! We can put together everything of the asymmetric encryption in PHP and it will look like this.

<?php
//Initiate the keys
$result = openssl_pkey_new(array("private_key_bits" => 4096));
//Export private key to $privateKey
openssl_pkey_export($result, $privateKey);
//Get the public key
$publicKey = openssl_pkey_get_details($result)['key'];
//Prepare your plain text to encrypt
$plaintext = "Welcome To Blogdesire";
//Encrypt and export encrypted value to $encrypted_text
openssl_public_encrypt($plaintext,$encrypted_text,$publicKey);
//Print encrypted text 
echo $encrypted_text;
//Decrypt the encrypted message using the private key
openssl_private_decrypt($encrypted_text, $plaintext, $privateKey);
//Print the decrypted message
echo $plaintext;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)