Debug School

rakesh kumar
rakesh kumar

Posted on

One_way Encryption and Two_way Encryption

php-encryption
php-two-way-encryption
php-encryption

One_way Encryption

By using this, we can encode the data but we cannot decode encoded data.

md5() function: (Message-Digest 5)
Enter fullscreen mode Exit fullscreen mode

By using this function, we can encode the data as 32 characters length, alphanumeric string.

Syntax
string md5 ( string $str [, bool $raw_output = FALSE ] )

Image description
Returns
md5() function returns the calculated md5 hash on success, or false on failure.

Example 1

<?php   
    $str = "sonoo";  
    echo md5($str);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Image description
Example 2

<?php  
$str = "sonoo";  
echo md5($str);  
if (md5($str) == "ea866df636e6d5b4b7c9ab7b596cdd4c")  
    {  
        echo "<br>Hello javatpoint";  
         exit;  
     }  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Two-way Encryption

By using this concept, we can encode and decode the data. In simple terms, two-way encryption means there is both encrypt and decrypt function present. In PHP, two-way encryption is accomplished through the following function.

base64_encode()
base64_decode()
Enter fullscreen mode Exit fullscreen mode

base64_encode()
This function is used to encode the given data with base64. This function was introduced in PHP 4.0.

Syntax

string base64_encode ( string $data ) 
Enter fullscreen mode Exit fullscreen mode

Parameters

Image description

Returns:
The base64_encode() function returns the encoded data as string.

Example 1

<?php  
    $str= "javatpoint";  
    $str1= base64_encode($str);  
    echo $str1;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Image description
Example 2

<?php  
    $str = 'Welcome to javatpoint';  
    echo base64_encode($str);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Image description
base64_decode():
The base64_decode() function is used to decode a base64 encoded data. This function was introduced in PHP 4.0.

Syntax

string base64_decode ( string $data [, bool $strict = FALSE ] ) 
Enter fullscreen mode Exit fullscreen mode

Parameters

Image description
Returns:
The base64_decode() function returns the decoded data or false on failure. The returned data may be binary.

Example 1

<?php  
    $str = 'V2VsY29tZSB0byBqYXZhdHBvaW50';  
    echo base64_decode($str);  
?>
Enter fullscreen mode Exit fullscreen mode

Output:

Image description
Example 2

<?php  
    $str= "amF2YXRwb2ludA==";  
    $str1= base64_decode($str);  
    echo $str1;  
?>  
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Top comments (0)