TechnologySeptember 30, 2015by think201_admin

String Encryption and Decryption technique using PHP

Ever wondered how the secret messages are maintained confidential and retrieved only by the receiver, What if I told you that a computer program could accomplish this for you. With the advent of a few new technologies, computer programs have emerged as a powerful tool to get even the most strategized and concept oriented task accomplished in no time.

I would take you through one such concept which is none other than the topic best suited for the present context. Yes, I would enrich your knowledge with the ‘String Encryption and Decryption technique’ that makes use of PHP.

Encryption is the process of rendering a message such that it becomes unreadable without possessing the correct key. In brief, we can categorise the Cryptography into two kinds, the Symmetric Cryptography and the Asymmetric Cryptography. Symmetric cryptography uses the same key for encryption as well as for decryption, whereas in the Asymmetric cryptography the message is encrypted using a public key and only those who possess the private key would be able to decrypt it.

This process has to be carried out in a scenario where we need to compute simple string encryption and decryption, and please be aware that this is not a relevant technique for password encryption.

Here goes the code for the technique explained above (Note that the code has been written using PHP).

function encrypt($value, $key)
  $text = $value;
  // getting the size of the IV (Initialization Vector) belonging to a specific cipher / mode combination
  // Accept two params
  	// 1: cipher (MCPYPT_ciphername constraint or the name of algorithms string)
  	// 2: mode (MCPYPT_MODE_modename constraint or one of the following string - ecb, cbc, cfb, ofb, nofb or stream )
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

  // Create an initialization vector(IV) from RANDOM source
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  // Encrypt plaintext with given params
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);

  return $crypttext;

function decrypt($value, $key)
  $crypttext = $value;
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  return trim($decrypttext);

// Get the file path or you can provide directly string as well
$File = getcwd().'/test.txt';

// Get the data from file
$Data = file_get_contents($File);

// Call encrypt function and provide the data and secure key as param
// Make sure that secure key should strong
$EncryptData = encrypt($Data, ‘SECURE_KEY’);

// Use decrypt function to decrypt the encrypted data 
// You have to provide the encrypted data which you want to decrypt and provide secure key
// You have to use same secure key to get the plain text data from ciphertext
$DecryptData = decrypt($EncryptData, ‘SECURE_KEY');