Skip to content

Instantly share code, notes, and snippets.

@napolux
Last active January 8, 2022 16:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napolux/0f62173c849941899956 to your computer and use it in GitHub Desktop.
Save napolux/0f62173c849941899956 to your computer and use it in GitHub Desktop.
Simple reversible encryption
<?php
// From a StackOverflow answer...
// I can't find the original link :-(
class StringEncryption {
const ENCRYPTION_KEY = "She's a killer queeeeeeeeen!";
public function encode($url = '') {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(self::ENCRYPTION_KEY), $url, MCRYPT_MODE_CBC, md5(md5(self::ENCRYPTION_KEY))));
}
public function decode($string = '') {
$return = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(self::ENCRYPTION_KEY), base64_decode($string), MCRYPT_MODE_CBC, md5(md5(self::ENCRYPTION_KEY))), "\0");
return (ctype_print($return)) ? $return : false;
}
}
@footballencarta
Copy link

The Mcrypt functions have been deprecated as of PHP 7.1.0 (https://www.php.net/manual/en/function.mcrypt-encrypt.php) - so I'd suggest against using this function for all currently supported versions of PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment