tags:

views:

28

answers:

2

I tried to encrypt an array then decrypt it back to string by calling a function, it's seem return the correct value if I does all encrypt and decrypt at once time in the function, however, if I return the encrypt value, then call the function again to decrypt it will return me some strange code.

Example 1:

public main()
{
    $dataArray = array("one"=>1, "two"=>2, "three"=>3);
    $a = $this->encryptDecryptInfo(json_encode($dataArray),$this->key);
    var_dump($a);
}

public function encryptDecryptInfo($text,$key)
{
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, 
    $text= base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv));
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CFB, $iv);       
}

This will return me the correct value which is string(27) "{"one":1,"two":2,"three":3}"

Example 2:

public main()
{
    $dataArray = array("one"=>1, "two"=>2, "three"=>3);
    $a = $this->encryptDecryptInfo(json_encode($dataArray),$this->key,"encrypt");       
    $b = $this->encryptDecryptInfo($a,$this->key,"decrypt");
    var_dump($b);
}

public function encryptDecryptInfo($text,$key,$type)
{
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
    if($type == "encrypt")
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv));
    else return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CFB, $iv);      
}

However if I do my code in this way, it will return me strange value which is like this string(27) "�ÔérôŸY éXgíœÈÐN*é౜CµÖ" .

Deos anyone know why this is happen? Both encrypt and decrypt coding are the same for example 1 and example 2, but why it will return strange code in example instead? Any way to fix this issue?

+1  A: 

I think this is encoding issue look for UTF here - http://php.net/manual/en/function.base64-encode.php in the comments there is a UTF8 safe encoding function.

By passing the parameters left and right you are changing the encoding and you loose it in the translation. Welcome to PHP :)

Ivo Sabev
@Ivo Sabev hmm... any way I can fix this problemn??
Jin Yong
Look at the comments of the link I attached. You can either change your files to be ANSI if you dont really need the UTF or look at the middle of the webpage I pointed out there is a UTF safe BASE64 class - http://www.php.net/manual/en/function.base64-encode.php#78765
Ivo Sabev
This has nothing to do with the encoding. Obviously the OP is using two different initialization vectors (IV) to en- and to decrypt the string - that won't work. Please see stereofrog's answer about saving the IV together with the encrypted data.
Stefan Gehrig
+1  A: 

You must use the same IV for decryption. Just save it along with encrypted data, for example:

if($type == "encrypt") {
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
    return base64_encode($iv . '@@' .
        mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv));
} else {
    list($iv, $data) = explode('@@', base64_decode($text));
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CFB, $iv);      
}
stereofrog