PHP AES 256-bit Encryption and Decryption

Secure your data using PHP and AES 256 bit algorithm. Encrypt entire pain text to a cipher text and decrypt using the same key pairs.
Secure Your Data with AES sha256 bit Encryption and Decryption method using PHP
811 Views

Hi everyone, 
So today let’s discuss another method of data encryption and decryption using PHP AES 256-bit and OpenSSL to cipher[encryption] and decipher[decryption] any string in the PHP.

AES (Advanced Encryption Standard) is a symmetric key encryption that has been approved by the US National Institute of Standards and Technology (NIST) as FIPS 197 in 2001.

AES is a subset of the Rijndael cipher developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen. Rijndael is a family of block ciphers with a variable block length and a variable key length.

The AES algorithm supports 128, 192, and 256-bit encryption, which can be implemented in software or hardware. The most common length used is 128-bit, which has been broken by academic cryptanalysts.

256-bit encryption is the strongest and most robust form of encryption available today. That being said, even 256-bit encryption is not unbreakable. In fact, it has been theorized that if all current computers in the world were put to work on the task, it could be possible to break AES-256 encryption in as little as 20 years.

Encryption is a common method to protect data in the cyber world, which converts plain text into a long ciphertext constructed with random alphanumeric characters.

There are other methods to encrypt & decrypt using PHP functions for example openssl_pkey.

We are using AES Advanced Encryption Standard with a 256-bit key length. The following image shows how symmetric key encryption works:

AES Encryption
Illustration of the AES 256 Cipher & Decipher [Symmetric Key Encryption] 

Let’s see the main recipe of the topic:

class secureToke
{
    private static $secretKey = 'Your Desired key(Hash)';
    private static $secretIv = 'www.domain.com';
    private static $encryptMethod = "AES-256-CBC";
    public static function tokenencrypt($data)
    {
        $key = hash('sha256', self::$secretKey);
        $iv = substr(hash('sha256', self::$secretIv), 0, 16);
        $result = openssl_encrypt($data, self::$encryptMethod, $key, 0, $iv);
        return $result = base64_encode($result);
    }
    public static function tokendecrypt($data)
    {
        $key = hash('sha256', self::$secretKey);
        $iv = substr(hash('sha256', self::$secretIv), 0, 16);
        $result = openssl_decrypt(base64_decode($data), self::$encryptMethod, $key, 0, $iv);
        return $result;
    }
}

Encryption method:

$tokenencrypt = secureToken::tokenencrypt($token);

Decryption method:

$tokendecrypt = secureToken::tokendecrypt($encryptedtoken);

Voila! it’s done… Now you can encrypt your entire data on the webspace and make them secure. 🙂

Total
0
Shares
Previous Post
Python Desktop GUI

Python Desktop GUI – Part 1

Next Post
Minify HTML, CSS, JS WordPress Site using simple PHP function to boost your page speed and performance without any WordPress plugins.

WordPress – Minify CSS, HTML, JS files using PHP

Related Posts