Encryption in PHP leaves unwanted characters

Posted by MichaelH on Stack Overflow See other posts from Stack Overflow or by MichaelH
Published on 2012-10-25T10:45:31Z Indexed on 2012/10/25 11:00 UTC
Read the original article Hit count: 205

Filed under:
|

I have made an encryption function which encrypts a simple value and stores it in the database. Here is the code to encrypt and decrypt:

public function encrypt($string){
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $value = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key256, $string, MCRYPT_MODE_ECB, $iv);
    $value = base64_encode($value);
    return $value;
}

public function decrypt($string){
    $value = base64_decode($string);
    $value = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key256, $value, MCRYPT_MODE_ECB);
    return $value;
}

When I encrypt a simple value such as 'Michael' and decrypt again, I get the value:

Michael?????????

Is there a reason I get all those question marks or a way to get rid of them?

© Stack Overflow or respective owner

Related posts about php

Related posts about encryption