Question

Can someone help me with this:

When I try to mcryp 253|someonesnew@gmail.com.. and then decrypt the output .. it returns 253|someonesnew@gmail.co��o{�

Strange thing: When I change any given character, the output is always with a strange char.. except.. if I just add one character or remove one.. the output is good..

How is this possible?

 function url_base64_decode($str){
    return base64_decode(strtr($str,
        array(
        '.' => '+',
        '-' => '=',
        '~' => '/'
        )
    ));
}
function url_base64_encode($str){
    return strtr(base64_encode($str),
        array(
            '+' => '.',
            '=' => '-',
            '/' => '~'
        )
    );
}
function mdecrypt($input){
        $key = '4oF9B2N_WXbmvIC5nNLLTbnmr5knkEBNBcrJt9m3xM3kjFyCZc3QAZbolXoCHO3g';
    $input = trim(chop(url_base64_decode($input)));

    $td = mcrypt_module_open ('tripledes', '', 'ecb', '');
    $key = substr(md5($key),0,24);
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
    mcrypt_generic_init ($td, $key, $iv);
    $decrypted_data = mdecrypt_generic ($td, $input);
    mcrypt_generic_deinit ($td);
    mcrypt_module_close ($td);
    return trim(chop($decrypted_data));
}



function mencrypt($input) {
    $key = '4oF9B2N_WXbmvIC5nNLLTbnmr5knkEBNBcrJt9m3xM3kjFyCZc3QAZbolXoCHO3g';
    $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 = mcrypt_generic ($td, $input);

    mcrypt_generic_deinit ($td);
    mcrypt_module_close ($td);
    return trim(chop(url_base64_encode($encrypted_data)));
}

$str = mencrypt('253|someonesnew@gmail.com'); // oCaWMrNIAX6wCS.HSlvFGspMLauXaSyAE.ze7j9q4Qk-
echo mdecrypt($str);
Was it helpful?

Solution

This is because of your key.

This is your key:

$key = '4oF9B2N_WXbmvIC5nNLLTbnmr5knkEBNBcrJt9m3xM3kjFyCZc3QAZbolXoCHO3g';

Remove "_" from it. And you will get ok result.

I had same problem and I solve that with removing "_" or any other special character from my key.

Phpfiddle url: http://phpfiddle.org/main/code/m95-4eb with working demo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top