Stack Exchange
mcrypt_encrypt garbled text
First, excuse the question, it might be a simple problem, but I have troubles understanding the encryption methods..
I'm using the following functions to encrypt / decrypt:
private function encodemc($value,$skey){ 
    if(!$value){return false;}
    $skey = substr($skey, 2, 4);
    $text = $value;
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $skey, $text, MCRYPT_MODE_ECB, $iv);
    return trim($this->safe_encode($crypttext)); // safe_encode adds another encoding using `base64_encode`
}

private function decodemc($value,$skey){
    if(!$value){return false;}
    $skey = substr($skey, 2, 4);
    $crypttext = $this->safe_decode($value);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $skey, $crypttext, MCRYPT_MODE_ECB, $iv);
    return trim($decrypttext);
}

The $key looks like this: 570c45546dwq45gjk191.
I pass the value to be encrypted to the first function, then I save it to the db, and then I retrieve it from the db, I decrypt it and show it as html text.
The problem is that some text does not get decrypted/encrypted right, and it displays in the html page as if it was in the wrong text encoding.
The weird part is that out of 10 items, only 2 or 3 are garbled, depending on the key.
In addition, sometimes only a portion of the string is garbled.
I've found out that what causes the garbling are some random letters. For instance, when using the above key the letter S breaks the code and the text gets garbled.
So then I've applied substr($skey, 2, 4); to the key to see if anything changed. Turns out that if I change the key the characters that break the code are different.
But even with a key of lenght == 1 the problem persists.
Any idea on what's the problem?
EDIT:
Here the rest of the code.
private function safe_encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}

private function safe_decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return stripslashes(base64_decode($data));
}


We need the code from the safe_encode and safe_decode functions as well.  I'm going to wager that it's either them or the trim that's breaking things.

If those two safe_en/decode functions would only do base64 en/decode operations, you woulnd't have created functions for it, would you?

stripslashes!  If you ever find yourself needing it, something has gone wrong.

I've updated the question with the missing code.

It's never needed.  Never, ever.  Not unless you have magic quotes enabled, which you'd better well not in 2013...

@Charles You were totally right.. Not only I didn't need it, THAT was the problem!!!! I can't believe I passed so much time trying to understand how the encryption method was working, when the problem was so "silly". Thank's really a lot!

Unfortunately your encryption sucks for a security point of view, even if it may "work" now. For example you're using ECB. Once you replace that with something better, you'll notice that it doesn't work because you're using different IVs for encryption/decryption.

@CodesInChaos I've come up with that code looking at the manuals and here on SOF. As I said I'm not an expert on encryption, can you explain what the problem is with the code? How could I improve it?

That seems like a very weird issue.
I do not have a definite answer for you, but here are a couple of things you can try.

Encrypt/Decrypt without going to the database.  If the issues goes away, then there is a problem with the database handling the characters output though the encryption function.
Trim the value before you encrypt it. If the issues goes away, then the trim after encryption is wrong.
Remove the safe_encode and safe_decode code. If the issue goes away, then these functions are adding/removing important things.

It is important that you do all the above in one go, as there might be multiple issues. If the above steps remove the issues, then reintroduce them one at a time to identify which is causing the problem.

OP states that those functions are base64 encodings only. If so, the points you list are moot, maybe not 3 but this sounds more like a comment than an answer then. Yes the question is not complete, so not entirely your "fault".

I've tried to skip the safe_decode/encode functions, but them the text was even more garbled...
