Home > Backend Development > PHP Tutorial > PHP mcrypt encryption and decryption function

PHP mcrypt encryption and decryption function

WBOY
Release: 2016-08-08 09:25:44
Original
1263 people have browsed it

加密

function mcrypt_encode ($data, $key) {
    $init_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
    $data = $init_vect . mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $init_vect);
    return $data;
}
Copy after login

解密
function mcrypt_decode ($data, $key) {
    $init_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    if ($init_size > strlen($data)) {
        return false;
    }
    $init_vect = substr($data, 0, $init_size);
    $data = substr($data, $init_size);
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $init_vect), "\0");
}
Copy after login

以上就介绍了PHP mcrypt 加密解密函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template