Jump to content

Decryption function doing something weird with encoding


Speedysnail6
Go to solution Solved by mac_gyver,

Recommended Posts

Hi. This is a function to decrypt text. Please do not worry about the masterkey, that is called upon from outside.

//Decryption Function
function decrypt($mprhase) {
    global $encryptionkey;
    $MASTERKEY = $encryptionkey;
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $MASTERKEY, $iv);
    $decrypted_value = mdecrypt_generic($td, base64_decode($mprhase));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $decrypted_value;
}

Although this works normally, it does decrypt text with some weird encoding or something. I've tried decrypt() multiple times in an IF statement and was very confused why it does not work. It seems to look alike in code and in words when I echo it directly on a page, but when I tried putting the decrypt content in a textarea, and I get

Decrypted Text���

in the textarea. What is my decription function doing with the encoding or whatever, and please tell me how to correct it.

 

Thanks in advance and please tell me if I am not being clear enough.

Link to comment
Share on other sites

Thanks! I used trim() and it worked.

New code is...

//Decryption Function
function decrypt($mprhase) {
    global $encryptionkey;
    $MASTERKEY = $encryptionkey;
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $MASTERKEY, $iv);
    $decrypted_value = mdecrypt_generic($td, base64_decode($mprhase));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return trim($decrypted_value);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.