Search code examples
javascriptphpmcryptblowfish

Decrypt blowfish in php


I'm using blowfish.js on my clientside. To encrypt on the clientside I use

var encryptedData = blowfish.encrypt(
    JSON.stringify(myData),  myEncryptionKey, {cipherMode: 0, outputType: 0}
);

That way encryptedData is Base64 encoded blowfish string. It is OK.

To decrypt on the clientside I use

var decryptedData = blowfish.decrypt(
     encryptedData , myEncryptionKey, {cipherMode: 0, outputType: 0}
);

So decryptedData equals to JSON.stringify(myData). It is OK.

Then I send my encryptedData to the .php script that contains

$data = $_POST['data'];
$data = mcrypt_decrypt(MCRYPT_BLOWFISH, $myEncryptionKey, pack('H*',$data), MCRYPT_MODE_ECB);

And when I try to print $data I'm getting something unreadable like

�Nv��Xd�̿�:q6��A����,!v��c�O¡1�%>��ҭ� {0�� �    ���g�u�����t�z3q$����T��/Ҧ.j-�/!���;�lS���Z�X
��&����{j�m�����F�`7��.......and so on

It isnt OK. Does anyone know what I'm doing wrong on the server-side?


Solution

  • Why do you pack('H*',$data) as you wrote the data is a base64 encoded string. Just base64_decode the data and pass it to the decrypt function.

    $data = $_POST['data'];
    $decryptedData = base64_decode($data);
    
    $data = mcrypt_decrypt(MCRYPT_BLOWFISH, $myEncryptionKey, $decryptedData, MCRYPT_MODE_ECB);