«

»

Feb 17

Easy-to-use and strong Encrypt/Decrypt PHP functions

I wrote those two following PHP functions to encrypt and decrypt strings easly and with a stronger encryption module than the other examples on the net (please, note that “strong” does not equals “secure”!);

Happy encryption/decryption! :)

Max

	function encrypt($input_string, $key){
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$h_key = hash('sha256', $key, TRUE);
		return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $h_key, $input_string, MCRYPT_MODE_ECB, $iv));
	}

	function decrypt($encrypted_input_string, $key){
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$h_key = hash('sha256', $key, TRUE);
		return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $h_key, base64_decode($encrypted_input_string), MCRYPT_MODE_ECB, $iv));
	}

6 comments

  1. Mike

    This does look extremely “strong” as I am having a tough time understanding most of the code there. Can you just clarify what the $input_string and $key variables are?
    Thanks
    Mike

    1. mitzip

      $input_string is the text you wish to encrypt
      $key is your “password” or “passphrase”

      1. maxvergelli

        exactly ;)

  2. bagusjavas

    Thanks Max..

    I Like it..

    Let me use it :D

    1. maxvergelli

      Free to use it!
      :)

  3. Joe

    Thank you maxvergelli, this is very useful..!
    Thanks.. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>