Encryption in PHP leaves unwanted characters
- by MichaelH
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?