Skip to content

Instantly share code, notes, and snippets.

@pentium10
Created July 20, 2011 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pentium10/30aed5ff847b8c9223c4 to your computer and use it in GitHub Desktop.
Save pentium10/30aed5ff847b8c9223c4 to your computer and use it in GitHub Desktop.
<?php
function decrypt($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = $this->decrypt($value);
}
return $data;
}
if (empty($data) || USE_ENCRYPTION == false) {
return $data;
}
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$key = substr('mykey', 0, mcrypt_enc_get_key_size($cipher));
$iv = '00000000';
// 128-bit Blowfish encryption:
if (mcrypt_generic_init($cipher, $key, $iv) != - 1) {
// PHP pads with NULL bytes if $cleartext IS NOT a multiple of the block size ..
$data = base64_decode($data);
if (empty($data)) {
return '';
}
$cleartext = mdecrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
}
// Remove the padding.
$pad = ord(substr($cleartext, strlen($cleartext) - 1));
if ($pad > 0 & $pad <= 8) {
$cleartext = substr($cleartext, 0, strlen($cleartext) - $pad);
}
return urldecode($cleartext);
}
function encrypt($cleartext) {
if (is_array($cleartext)) {
foreach ($cleartext as $key => $value) {
$cleartext[$key] = $this->encrypt($value);
}
return $cleartext;
}
if (empty($cleartext) || USE_ENCRYPTION == false) {
return $cleartext;
}
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$key = substr('mykey', 0, mcrypt_enc_get_key_size($cipher));
$iv = '00000000';
// add the padding.
$cleartext_length = strlen($cleartext) % 8;
for ($i = $cleartext_length; $i < 8; $i++) {
$cleartext .= chr(8 - $cleartext_length);
}
$ciphertext = '';
// 128-bit Blowfish encryption:
if (mcrypt_generic_init($cipher, $key, $iv) != - 1) {
// PHP pads with NULL bytes if $cleartext IS NOT a multiple of the block size ..
if (empty($cleartext)) {
return '';
}
$ciphertext = mcrypt_generic($cipher, $cleartext);
mcrypt_generic_deinit($cipher);
}
return base64_encode($ciphertext);
}
echo encrypt('thevalue'); // wTHzxfxLHdMm/JMFnoh0hciS/JADvFFg
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment