php – Encryption & Decryption

I used php while developing an alumni website of my college. It is not the official alumni website but a non-official nostalgic effort by some of us (6 friends of college days).

I needed some encryption technique in php for encrypting some of the data provided by user and then get it back by them to verify the account. I used following easy functions to do that. You need to provide a pass phrase to encrypt and decrypt your string.

You can make encryption more secure by keep changing pass phrase so that no one can guess that.

 
//encodes the string. Returns an array with the
//string as the first element and the initialization
//vector as the second element
function easy_crypt($string, $key){
	$iv_size = @mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
	$iv = @mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
	$string = @mcrypt_encrypt(MCRYPT_BLOWFISH, $key,$string, MCRYPT_MODE_CBC, $iv);
	return array(@base64_encode($string), @base64_encode($iv));
}
 
//decodes a string
//the first argument is an array as returned by easy_encrypt()
function easy_decrypt($cyph_arr, $key){
	$out = @mcrypt_decrypt(MCRYPT_BLOWFISH, $key, @base64_decode($cyph_arr[0]),
                         MCRYPT_MODE_CBC, @base64_decode($cyph_arr[1]));
	return @trim($out);
}
$code = 'tech1egy@n@p@$$word__132465798';
 
 
$cypher = easy_crypt($string_to_encrypt, $code);
$encrypted_string = $cypher[0];
 
$original_string = easy_decrypt($cypher, $code);
 
 

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)