Encrypt and Decrypt String in PHP

Home > PHP Object Oriented > Encrypt and Decrypt String in PHP



How to Encrypt and Decrypt String in PHP

In this tutorial We are going to create a Jumbler Class which allows users to encrypt and decrypt text using a simple encryption algorithm and a user-supplied numeric key.

Take a look a the class Definition (jumbler.php)

<?php 
//calss definition
class jumbler
{
	//define public properties key
	public $key;
	
	//define a method to set encrtyption key
	public function setKey($key)
	{
	$this->key=$key;
	}
	
	//define a method to get  encrtyption key
	public function getKey()
	{
	return $this->key;
	}
	
	//define a method to encrypt a string
	public function encrypt($plainText)
	{
		for($x=0;$xgetkey()+($x*$this->getkey());
		}
		return implode("/",$cipher);
	}
	
	//define a method to decrypt a string
	public function decrypt($cipher)
	{
		$data=explode("/",$cipher);
		$plainText='';
		for($x=0;$xgetkey()-($x*$this->getkey()));
		}
		return $plainText;
	}
	
}
?>

Call Jumbler Class

<?php 
//call submit button
extract($_REQUEST);
if(isset($submit))
{
$obj= new jumbler();
$obj->setKey($key);
	if($chk=="C")
	{
	echo $obj->decrypt($text);	
	}
	else
	{
	echo $obj->encrypt($text);	
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Encrypting and Decrypting Text</title>
</head>
<body>
	<form method="post">
	<table>
		<tr>
			<td>Select Your Choice</td>
			<td>
				<input type="radio" name="chk" value="P">Encrypt
				<input type="radio" name="chk" value="C">Decrypt
			</td>
		</tr>
		<tr>
			<td>Enter Your Data</td>
			<td><textarea rows="6" name="text" cols="25"></textarea></td>
		</tr>

		<tr>
			<td>Enter Numeric Key</td>
			<td><input type="number" name="key"></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<input type="submit" name="submit" value="submitt">
			</td>
		</tr>
	</table>
	</from>
</body>
</html>
Output

Explanation :

This class has a single Property and four methods:

  • The $key : Property holds the numeric key entered by the user. This key is used to perform the encryption.
  • The setKey( ) : method accepts an argument and sets the $key property to the that value.
  • The getKey( ) : method returns the value of the $key property.
  • The encrypt( ) : method accepts a plain-text string and “Jumbles ” it using the key.
  • The decrypt( ) : method accepts an encrypted string and restores the original plain-text string from it using the key.

A Quick word about the internal of the encrypt( ) and decrypt( ) methods, before proceeding to a usage example . When encrypt( ) is invoked with a plain-text string, it steps through the string and calculates a numeric value of each character. The numeric value is the sum of

  • The Character’s ASCII code, as returned by the ord( ) function
  • The numeric key set by the use through the setKey( ) method
  • The product of the numeric key and the character’s position in the string.

Each number returned after this calculation is added to an array, and once the entire string is processed, the elements of the array are joined into a single string, separated by slashes, with implode( ).