Cross-Platform, PHP

Simple AES Encryption with PHP

Today we will see how we can use mcrypt_encrypt PHP function to encrypt a plain text using AES 128!

1
2
3
4
5
6
7
8
function encrypt($plaintext, $key)
{
	$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, 
			$key, 
			$plaintext, 
			MCRYPT_MODE_ECB);
	return base64_encode($crypt);
}

Usage:

$cipher_text = encrypt("my name is George!", $key_128bit);

You can produce an appropriate 128 bit key in this way:

$key_128bit = md5("my precious secret key");