mcrypt 问题

WBOY
Release: 2016-06-06 20:40:56
Original
1202 people have browsed it

Function

<code>function mcrypt($key, $input,$mode = 'E'){

    $key = substr(md5($key), 0, 24);
    $td = mcrypt_module_open('tripledes','','ecb','');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);

    $encrypted_data = '';
    //进行加密
    if(strtoupper($mode) == 'D')
    {
        $encrypted_data = mdecrypt_generic($td, $input);

    }
    else
    {
        $encrypted_data = mcrypt_generic($td, $input);

    }

    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return $encrypted_data;
}
</code>
Copy after login
Copy after login

var_dump()

<code>var_dump(mcrypt('122223','121212'));
</code>
Copy after login
Copy after login

结果

<code>string 'b��t��' (length=8)
</code>
Copy after login
Copy after login

怎么解决这个问题? - _ -|||

回复内容:

Function

<code>function mcrypt($key, $input,$mode = 'E'){

    $key = substr(md5($key), 0, 24);
    $td = mcrypt_module_open('tripledes','','ecb','');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);

    $encrypted_data = '';
    //进行加密
    if(strtoupper($mode) == 'D')
    {
        $encrypted_data = mdecrypt_generic($td, $input);

    }
    else
    {
        $encrypted_data = mcrypt_generic($td, $input);

    }

    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return $encrypted_data;
}
</code>
Copy after login
Copy after login

var_dump()

<code>var_dump(mcrypt('122223','121212'));
</code>
Copy after login
Copy after login

结果

<code>string 'b��t��' (length=8)
</code>
Copy after login
Copy after login

怎么解决这个问题? - _ -|||

你可以使用base64_encode,解密时使用base64_decode.

加密完是二进制,显示不了,所以要用base64_encode,base64_decode

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