Hi,
I've read up all I could find on using the Pear Blowfish class, but am hitting a conceptual wall.

I have no trouble creating an instance of the class, setting a "key", encrypting some plain text, and decrypting the text FROM WITHIN THE SAME CLASS INSTANCE.

But, I plan to store the bin2hex of the encrypted text in a database, then later decrypt using the correct key. But when testing the following -- using one instance to encrypt and another instance to decrypt -- I can't seem to get my plaintext back. Instead I just get an error object saying my key is not initialized.

<code>
<?php
require_once 'Crypt/Blowfish.php';

/ Create the Crypt_Blowfish object using a secret key /
$in = new Crypt_Blowfish('secret key');

/ Encrypt some plain text /
$encrypted = $in->encrypt('secret content to be inserted into database');

/ Decrypt the secret message using a different instance of the class /
$out = new Crypt_Blowfish('secret key');
$decrypted = $out->decrypt(bin2hex($encrypted));

/ Echo the results /
echo 'Decrypted: ' . $decrypted;
?>
</code>

Shouldn't this work? Or am I misunderstanding some vital concept of 2-way encryption?

Any help at all would be greatly appreciated.

    Try losing the bin2hex() call you have in there.

      Hi WeedPacket --

      Thanks for the reply.
      Without the bin2hex, I have the same problem -- it returns an error object with, among other things, the following error:

      "The key is not initialized."

        Curious. I try this code (compare it with yours):

        <?php
        require_once 'Crypt/Blowfish.php';
        
        /* Create the Crypt_Blowfish object using a secret key */
        $in = new Crypt_Blowfish('secret key');
        
        /* Encrypt some plain text */
        $encrypted = $in->encrypt('secret content to be inserted into database');
        
        /* Decrypt the secret message using a different instance of the class */
        $out = new Crypt_Blowfish('secret key');
        $decrypted = $out->decrypt($encrypted);
        
        /* Echo the results */
        echo 'Decrypted: ' . $decrypted;
        ?>

        And the output I get is:

        Decrypted: secret content to be inserted into database

          Hi WeedPacket-

          Wow. Really appreciate that.

          I tried the code in another environment and see that it indeed works.

          Must be something about my local PHP install.

          Whatever it is, I can't imagine what would cause it to work within a class instance, but not across two instances.

          I'm using PHP 5 in both environments, (so not something about how PHP 4 v. PHP 5 deal with object references) and the Blowfish class doesn't require the Mcrypt extension.

          But at least I now know it's not the code. I was stuck on that, so thanks for getting me investigating in another direction!

            Hi Weedpacket,
            I had the same problem as larry98765, so I tried the code you quoted.
            Unfortunately it didn't work for me.
            I did wonder if it might be that I'm usinf PHP 4.
            It appears to be that a class var called _P is not defined, though why when it is other times I don't know.

            Here's the code I used ( I added some more echos )

            <?php
            require_once 'Crypt/Blowfish.php';

            / Create the Crypt_Blowfish object using a secret key /
            $in = new Crypt_Blowfish('secret key');

            / Encrypt some plain text /
            $PlainText = 'some secret content to be inserted into database';
            echo 'PlainText: ' . $PlainText.'<br>';

            $Encrypted = $in->encrypt($PlainText);
            if (PEAR::isError($Encrypted)) {
            echo "Encrypted: ".$Encrypted->getMessage();
            exit;
            }
            echo 'Encrypted: ' . $Encrypted.'<br>';

            / Decrypt the secret message using a different instance of the class /
            $out = new Crypt_Blowfish('secret key');

            $Decrypted = $out->decrypt($Encrypted);
            if (PEAR::isError($Decrypted)) {
            echo "Decrypted: ".$Decrypted->getMessage();
            exit;
            }
            / Echo the results /
            echo 'Decrypted: ' . $Decrypted.'<br>';
            ?>

            Here's the output.

            PlainText: some secret content to be inserted into database
            Encrypted: Kv éíe“bÒ:ó_tõ~øµµr«ýÖ¼tԍ™r±TÅTnŠ9iV:̉â€ß³
            Decrypted: The key is not initialized.

              Cerdaf --

              Are you using MAMP (Mac Apache MySQL PHP)? That was the environment I had the trouble with.

                Linux hercules.xssl.net 2.6.9-42.ELsmp #1

                  7 months later

                  Cerddaf probably already figured this out, but here's how we fixed this issue:

                  At least on our servers, the built-in PEAR mcrypt was causing the failures. We simply installed the mcrypt PHP5 module (make sure you have the libmcrypt package installed on your server as well). Everything worked perfectly after that.

                    Write a Reply...