Encrypt:
|
1 2 3 4 |
function encrypt($string, $key) { return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key)))); } |
Decrypt:
|
1 2 3 4 |
function decrypt($string, $key) { return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0"); } |
Key can be specific for whatever application you’re using the encryption for. For example, if you’re using it to encrypt passwords you would do something along the lines of:
|
1 |
encrypt($_POST['password'], "userpassword"); |
Now whenever you wanted to decrypt the user password you would have to use the same key:
|
1 |
decrypt($passwordstr, "userpassword"); |
Which leaves the functions open to an infinite number of applications.