Blowfish Encryption not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Blowfish Encryption not working

Post by aliasxneo »

I would put this in security but it's more code than theory.

Code: Select all

public function encryptBlowFish($key, $data)
    {
        $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
        $data = mcrypt_encrypt(MCRYPT_BLOWFISH, $key,
                                $data, MCRYPT_MODE_CBC, $iv);
    
        return array(base64_encode($data), base64_encode($iv));
    }
    
    public function decryptBlowFish($cyph, $key)
    {
        $out = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, base64_decode($cyph[0]),
                         MCRYPT_MODE_CBC, base64_decode($cyph[1]));
 
        return trim($out);
    }
Those are my two functions. Here they are in use:

Code: Select all

$test = $cell->protein->getProtein("Encrypt");
$cyph = $test->encryptBlowFish("mypass", "mysecmessage");
echo $test->decryptBlowFish($cyph, "mypass");
The result is nothing. I don't really see what I'm doing wrong though. The first function produces a cipher which consists of the encoded encrypted message and the encoded IV string in an array. The second function (decrypt function) takes that cypher along with the key and attempts to decrypt the message. Cans someone explain where I went wrong? Thanks.
Post Reply