How to decrypt Blowfish algorithm in php?

I have to write a PHP script to decrypt the encrypted data of Blowfish.

The data that I receive for decryption is encrypted by another application (I do not have access to it).

The data decrypts perfectly when I test it with a javascript script (blowfish.js).

How can i decrypt data in php?

I tried the function mcryptin PHP. The code works fine if I encrypt and decrypt using the same code. If I decrypt the encrypted code (in another application), it gives junk.

I don’t know which mode to set.

Can anyone suggest the code below or any PHP BlowFish code without using mcrypt?

<?php

class Encryption
{
    static $cypher = 'blowfish';
    static $mode   = 'cfb';
    static $key    = '12345678';

    public function encrypt($plaintext)
    {
        $td = mcrypt_module_open(self::$cypher, '', self::$mode, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, self::$key, $iv);
        $crypttext = mcrypt_generic($td, $plaintext);
        mcrypt_generic_deinit($td);
        return $iv.$crypttext;
    }

    public function decrypt($crypttext)
    {
        $plaintext = "";
        $td        = mcrypt_module_open(self::$cypher, '', self::$mode, '');
        $ivsize    = mcrypt_enc_get_iv_size($td);
        $iv        = substr($crypttext, 0, $ivsize);
        $crypttext = substr($crypttext, $ivsize);
        if ($iv)
        {
            mcrypt_generic_init($td, self::$key, $iv);
            $plaintext = mdecrypt_generic($td, $crypttext);
        }
        return $plaintext;
    }
}



$encrypted_text = Encryption::encrypt('this text is unencrypted');
 echo "ENCRY=".$encrypted_text;echo "<br/>";

////I am using this part(decryption) coz data already encryption 
// Encrypted text from app 
$encrypted_text = '29636E7ADA7081E7F5D73121C45E20D5';
// Decrypt text
$decrypted_text = Encryption::decrypt($encrypted_text);
  echo "ENCRY=".$decrypted_text;echo "<br/>";

?>
+3
source share
3 answers

$iv, , , , , , IV (return $iv.$crypttext;), .

, IV , . decrypt IV , .

, , encrypt . ( ), IV , .

, , (CFB) .

+1

There is a PEAR library for creating collisions that allows you to select the weather for using MCRYPT Libs or just your own PHP:

you can view the library here: Crypt_Blowfish 1.1.0RC2

Choose a PHP.php file that will show you the source code for this hard coding in native PHP.

0
source

All Articles