Search code examples
phpencryptioncryptographymcrypt

PHP CAST-256 mcrypt output differs


I am attempting to encrypt a string using CAST256 and CBC, via the PHP function mcrypt_encrypt. I am using the key test with the input test, which produces the following code:

mcrypt_encrypt(MCRYPT_CAST_256, 'test', 'test', MCRYPT_MODE_CBC);

The base64 encoded version of this produces (on PHP version 5.5.12):

DaypOCFVfoI8ghemj0ZkEg==

However, I am comparing my output against the tool on http://www.tools4noobs.com/online_tools/encrypt/, and my output differs significantly; the site output using the aforementioned cipher, mode, key, and data is the following:

eIKnQGAhjsGh+11XZsA2Lg==

Decrypting each string using the opposite tool (i.e. the site output decrypted with PHP, and the PHP output decrypted via the site) gives the following output:

DUCD000000000000 (site output)
DUCD000000000000 (PHP output)

However, decrypting using the same medium as the string was encrypted with gives the input data ('test').

My question is, is there a reason for this difference, such as omission of IV when encrypting/decrypting or a misuse of the PHP mcrypt_decrypt function?


Solution

  • This is most likely a bug in libmcrypt's cast-256 module, and the site that you've linked seems to be affected by it.

    I get the same output as you do on your local machine and the RFC2612 test vectors also pass on mine, so don't worry - it's not a mistake on your part, nor is something broken on your end.

    I do however have to say that you should never encrypt without using an IV and a proper encryption key ('test' is not a proper key). You should also use a more proven algorithm like AES.