这些函数将获取任何 PHP 对象并对其进行加密/解密:
加密 JSON 对象 Rijndael ECB base 64 编码
function ejor2eb($object, $key) {
// Encode the object
$object = json_encode($object, JSON_FORCE_OBJECT);
// Add length onto the encoded object so we can remove the excess padding added by AES
$object = strlen($object) . ':' . $object;
// Encrypt the string
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$result = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $object, MCRYPT_MODE_ECB, $iv);
// Return the URL encoded string, with the encryption type attached
return 'jor2eu:' . base64_encode($result);
}
解密 JSON 对象 Rijndael ECB base 64 解码
function djor2eb($string, $key, $default = false) {
// Remove the encryption type, and decode the string
$binary = base64_decode(substr($string, 7));
if (!$binary) {
return $default;
}
// Decrypt the string
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $binary, MCRYPT_MODE_ECB, $iv);
// Remove encrption padding
$tokens = null;
preg_match('/^([0-9]+):/i', $result, $tokens);
if (sizeof($tokens) !== 2) {
return $default;
}
$result = substr($result, strlen($tokens[1]) + 1, $tokens[1]);
// Decode the ecoded object
$object = json_decode($result);
return $object;
}