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.

  1. base64_encode()
  2. base64_decode()

1. base64_encode()

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

Syntax

snippet
string base64_encode ( string $data )

Parameters

Parameter Description Is compulsory
data The data to be encoded. compulsory

Returns:

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

Example 1

snippet
<?php
	$str= "rookienerd";
	$str1= base64_encode($str);
	echo $str1;
?>

Output:

Two-way Encryption

Example 2

snippet
<?php
	$str = 'Welcome to rookienerd';
	echo base64_encode($str);
?>

Output:

Two-way Encryption

2. base64_decode():

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

Syntax

snippet
string base64_decode ( string $data [, bool $strict = FALSE ] )

Parameters

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

Returns:

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

Example 1

snippet
<?php
	$str = 'V2VsY29tZSB0byBqYXZhdHBvaW50';
	echo base64_decode($str);
?>

Output:

Two-way Encryption

Example 2

snippet
<?php
	$str= "amF2YXRwb2ludA==";
	$str1= base64_decode($str);
	echo $str1;
?>

Output:

Two-way Encryption
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +