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

PHP mcrypt encryption and decryption function

WBOY
WBOYOriginal
2016-08-08 09:25:441273browse

加密

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;
}

解密
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");
}

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

Statement:
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