Mark Goldsmith

July 21, 2015

URL safe PHP encryption and decryption script

I've found myself often needing a simple encryption and decryption script that can be used in a URL, so no plus signs or equal signs or slashes. Here is one that might be of use to you.


public static function encryptIt( $pure_string ) {
	$key = '345fasdozdfasfoq345SDFDdfg';
	$iv = 23;
	$dirty = array("+", "/", "=");
    $clean = array("_p_", "_s_", "_e_");
	$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
	$_SESSION['iv'] = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
	$encrypted_string = base64_encode($encrypted_string);
	return str_replace($dirty, $clean, $encrypted_string);
}

public static function decryptIt( $encrypted_string ) {
	$key = '345fasdozdfasfoq345SDFDdfg';
	$iv = 23;
	$dirty = array("+", "/", "=");
	$clean = array("_p_", "_s_", "_e_");
	$string = base64_decode(str_replace($clean, $dirty, $encrypted_string));
	$decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $key,$string, MCRYPT_MODE_ECB, $iv);
	return $decrypted_string;
}


Share This

Be the first to comment

Post a comment