Home  >  Article  >  Backend Development  >  mcrypt 问题

mcrypt 问题

WBOY
WBOYOriginal
2016-06-06 20:40:561073browse

Function

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

var_dump()

var_dump(mcrypt('122223','121212'));

结果

string 'b��t��' (length=8)

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

回复内容:

Function

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

var_dump()

var_dump(mcrypt('122223','121212'));

结果

string 'b��t��' (length=8)

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

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

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

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