mcrypt_decrypt return strange code
        Posted  
        
            by Jin Yong
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jin Yong
        
        
        
        Published on 2010-04-09T04:49:43Z
        Indexed on 
            2010/04/09
            4:53 UTC
        
        
        Read the original article
        Hit count: 432
        
php
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.
Exmaple:
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}"
Exmaple 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?
© Stack Overflow or respective owner