PHP | OpenSSL Functions

PHP OpenSSL Functions


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP OpenSSL Functions.

OpenSSL

OpenSSL is a free open source module that is meant to take care of communications that take place over computer networks. OpenSSL is a tool for Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols.


SSL Certificate

A Secure Sockets Layer (SSL) is used by websites. The SSL certificate takes care of protecting the data between two computers by using encryption. The two computers involved can be the data sharing between the client and server. When you share data like password, credit card details, social security number, home address, it has to be protected and this is taken care of by the SSL certificate. The SSL certificate makes sure that the identity of both computers involved is authenticated for safe connection.

OpenSSL installation in PHP

OpenSSL module is by default added to PHP. You can activate the same by removing the (;) ;extension=php_openssl.dll added at the start of the extension in php.ini. After that restart apache and to confirm if the changes are reflecting save the below code as .php and execute the .php in browser.

<?php
   phpinfo();
?>

You should see the openssl enabled in the browser as shown below -

openssl

OpenSSL configuration

The openssl.cnf is the configuration file and has all the default configuration required for openssl to function. To execute openssl, the first thing is that PHP will try to locate the config file. To get the same you will have to add the PHP folder to environment variable.

If you are a Windows user, following below are the steps to setup environment variable for PHP folder -

  1. Right click on My Computer and then go to properties.
  2. Go to Advanced system Settings.
  3. Click on the 'Environment Variables' button.
  4. Edit the path variable and click on the Edit button.
  5. Now add the PHP folder path at the end.
  6. Once you are done, click on the Ok button.
  7. Open your command prompt and enter the command: openssl version -a.
C:\Windows\system32>openssl version -a
OpenSSL 1.0.2l  25 May 2017
built on: reproducible build, date unspecified
platform: mingw64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,2,long) idea(int) blowfish(idx)
compiler: x86_64-w64-mingw32-gcc -I. -I.. -I../include  -D_WINDLL -DOPENSSL_PIC
-DOPENSSL_THREADS -D_MT -DDSO_WIN32 -static-libgcc -DL_ENDIAN -O3 -Wall -DWIN32_
LEAN_AND_MEAN -DUNICODE -D_UNICODE -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DO
PENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSH
A512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM
 -DECP_NISTZ256_ASM
OPENSSLDIR: "/etc/ssl"

Now PHP will be able to locate the openssl.cnf configuration file.

OpenSSL Functions

The following table below list down all the PHP OpenSSL functions -

Sr.NoFunction & DescriptionVersion
1openssl_pkey_new()

Returns a resource identifier that has new private and public key pair

5.0.0
2openssl_pkey_get_private()

Returns the private key

5.0.0
3openssl_pkey_get_public()

Returns the public key

5.0.0
4openssl_​pkey_​export_​to_​file()

Exports the key to a file

5.0.0
5openssl_private_encrypt()

Encrypts the data with the private key

5.0.0
6openssl_public_encrypt()

Encrypts the data with public key

5.0.0
7openssl_public_decrypt()

Decrypts the data with the public key

5.0.0
8openssl_private_decrypt()

Decrypts the data with the private key


In our subsequent tutorials, we are going to be studying about the above list functions.


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be studying about the PHP openssl_pkey_new Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.