Get the App
SLTechnology News&Howtos  ›  Development  › 

Example usage of reversible encryption and decryption function in PHP

Shulou Source: shulou.com Published: 2022-06-03 19:00:01 10月01日 Update

This article mainly explains "reversible encryption and decryption function example usage in PHP". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "reversible encryption and decryption function example usage in PHP" together!

Function source code

The copy code is as follows:

function encrypt($data, $key) {

$prep_code = serialize($data);

$block = mcrypt_get_block_size('des', 'ecb');

if (($pad = $block - (strlen($prep_code) % $block))

< $block) { $prep_code .= str_repeat(chr($pad), $pad); } $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB); return base64_encode($encrypt); } function decrypt($str, $key) { $str = base64_decode($str); $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); $block = mcrypt_get_block_size('des', 'ecb'); $pad = ord($str[($len = strlen($str)) - 1]); if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) { $str = substr($str, 0, strlen($str) - $pad); } return unserialize($str); } 调用函数 复制代码 代码如下: $key = 'okyo.cn'; $data = array('id' =>

100, 'username' => 'customer', 'password' => 'e10adc3949ba59abbe56e057f20f883e');

$snarr = serialize($data);

$en = encrypt($data, $key);

$de = decrypt($en, $key);

echo "Encryption Prototype: ";

print_r($data);

echo "

Key: $key

Encryption result: $en

Decryption result: ";

print_r($de);

Thank you for reading, the above is the "reversible encryption decryption function instance usage in PHP" content, after the study of this article, I believe we have a deeper understanding of the reversible encryption decryption function instance usage in PHP, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Functions encryption encryption and decryption examples code learning content results prototypes keys that is ideas situations articles more source code knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Shulou Technology Docker NVidia