Return to Snippet

Revision: 50725
at September 4, 2011 04:16 by anagaiyahoocom


Updated Code
private $_key='some key';
       
       /**
	* Encrypt using mcrypt
	*
	* Value is encrypted with mcrypt and base64
	*
	* @param mixed $val Can be string or int
	* @return string Encoded value
	*/
	
	function mcryptEncode($val) {
		$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$enc = mcrypt_encrypt(MCRYPT_XTEA, $this->_key, $val, MCRYPT_MODE_ECB, $iv);
		$benc = base64_encode($enc);
		return $benc;
	}
	
	/**
	* Decrypt using mcrypt
	*
	* Takes value encrypted with mcrypt and base64 and decrypts 
	*
	* @param string $benc Encrypted mcrypt and base64 value
	* @return string Decoded value
	*/
	
	function mcryptDecode($benc) {
		$bdec = base64_decode($benc);
		$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$dec = mcrypt_decrypt(MCRYPT_XTEA, $this->_key, $bdec, MCRYPT_MODE_ECB, $iv);
		return $dec;
	}

Revision: 50724
at August 31, 2011 08:50 by anagaiyahoocom


Initial Code
/**
	* Encrypt using mcrypt
	*
	* Value is encrypted with mcrypt and base64
	*
	* @param mixed $val Can be string or int
	* @return string Encoded value
	*/
	
	function mcryptEncode($val) {
		$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$enc = mcrypt_encrypt(MCRYPT_XTEA, $this->_key, $val, MCRYPT_MODE_ECB, $iv);
		$benc = base64_encode($enc);
		return $benc;
	}
	
	/**
	* Decrypt using mcrypt
	*
	* Takes value encrypted with mcrypt and base64 and decrypts 
	*
	* @param string $benc Encrypted mcrypt and base64 value
	* @return string Decoded value
	*/
	
	function mcryptDecode($benc) {
		$bdec = base64_decode($benc);
		$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$dec = mcrypt_decrypt(MCRYPT_XTEA, $this->_key, $bdec, MCRYPT_MODE_ECB, $iv);
		return $dec;
	}

Initial URL

                                

Initial Description

                                

Initial Title
Encrypting and Decrypting with Mcrypt

Initial Tags

                                

Initial Language
PHP