Showing posts with label Decryption. Show all posts
Showing posts with label Decryption. Show all posts

Tuesday, January 23, 2018

Simple Two way Encryption and Decryption Function PHP



Hello guys, after a long time, I came back to my blog with new flavor of articles, and I am plan to concentrate my blog half my working day here after. So my followers can expect valuable development ideas from me through comment and feed.

Here we go our come back article, Here now I teach you the Two way encryption and decryption method with full explanation.

Objective Build a function for Two Way Encryption and Decryption:

            Encrypt as well as Decrypt our given string with our own Encrypt Decrypt Key and Initiative Vector (IV) (?), with Advanced Encryption Standard (AES) (?) this method is highly secure method all time ever with few PHP functions to build Two way Encryption and Decryption.

Functions Involved in Two Day Encryption and Decryption.

hash - Generate a hash value (message digest)
string hash ( string $algo , string $data [, bool $raw_output = false ] )

substr - Return part of a string
            string substr ( string $string , int $start [, int $length ] )

base64_encode - Encodes data with MIME base64
string base64_encode ( string $data )

base64_decode - Decodes data encoded with MIME base64
            string base64_decode ( string $data [, bool $strict = false ] )

openssl_encrypt - Encrypts data
string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )

openssl_decrypt - Decrypts data
            string openssl_decrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] ) 

Two Encryption & Decryption Function

/*
Written by: Jailani M, http://www.W3Lessons.com
Defineds:
ED_KEY - Encryption and Decription key your choice
IV_KEY - INITIATIVE VECTOR your choice
*/
define("ED_KEY","your_key");
define("IV_KEY","your_iv");

function w3_encode( $string) {
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $encoded_string = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
    return $encoded_string;
}

function w3_decode($string)
{
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $decoded_string = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
    return $decoded_string;
}



Download This Script     Live Demo     Download Script