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.
This function is used to encode the given data with base64. This function was introduced in PHP 4.0.
string base64_encode ( string $data )
Parameter | Description | Is compulsory |
---|---|---|
data | The data to be encoded. | compulsory |
The base64_encode() function returns the encoded data as string.
<?php $str= "rookienerd"; $str1= base64_encode($str); echo $str1; ?>
Output:
<?php $str = 'Welcome to rookienerd'; echo base64_encode($str); ?>
Output:
The base64_decode() function is used to decode a base64 encoded data. This function was introduced in PHP 4.0.
string base64_decode ( string $data [, bool $strict = FALSE ] )
Parameter | Description | Is compulsory |
---|---|---|
data | The encoded data. | compulsory |
strict | If the strict parameter is set to TRUE, then the base64_decode() function will return FALSE if the input contains character from outside the base64 alphabet. | Optional |
The base64_decode() function returns the decoded data or false on failure. The returned data may be binary.
<?php $str = 'V2VsY29tZSB0byBqYXZhdHBvaW50'; echo base64_decode($str); ?>
Output:
<?php $str= "amF2YXRwb2ludA=="; $str1= base64_decode($str); echo $str1; ?>
Output: