Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aligajani
Last active September 14, 2015 18:36
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 aligajani/52defdede379d89c1a19 to your computer and use it in GitHub Desktop.
Save aligajani/52defdede379d89c1a19 to your computer and use it in GitHub Desktop.
Base64 with Encryption
<?php
namespace Gajani;
class Base64_Encryption {
public static function aes128Encrypt($key, $data) {
if(16 !== strlen($key)) $key = hash('MD5', $key, true);
$padding = 16 - (strlen($data) % 16);
$data .= str_repeat(chr($padding), $padding);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16)));
}
public static function aes128Decrypt($key, $data) {
$data = base64_decode($data);
if(16 !== strlen($key)) $key = hash('MD5', $key, true);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16));
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
}
}
<?php
use Gajani\Base64_Encryption;
$key = "somethingStrong";
$encryptedText = Base64_Encryption::aes128Encrypt($key, 'aligajani');
$decryptedText = Base64_Encryption::aes128Decrypt($key, $encryptedText);
echo $decryptedText;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment