Question

When running this:

var_dump(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, '12345', '1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
var_dump(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, '12345', '2abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
var_dump(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, '12345', '3abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
var_dump(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, '12345', '4abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
var_dump(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, '12345', '5abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));

Its results in:

string 'X9VFmBzVWwUSoKPtE0VoEYxGkAPwVkUDQfQwF0wPDAVxedrGWE4oMCPBAF7FRBMoQsSUFwHhpknj/fHQqgQVPw==' (length=88)
string 'T/yn6lkyB73cEs57K2pUVNhFk2tDX02CbnBIU2c2I2VxedrGWE4oMCPBAF7FRBMoQsSUFwHhpknj/fHQqgQVPw==' (length=88)
string 'WysJWSV2k5gyYoQ4+hF0Uh2JqMJ6pTC8lcrgs1jmSklxedrGWE4oMCPBAF7FRBMoQsSUFwHhpknj/fHQqgQVPw==' (length=88)
string 'fMsJByfrZBYoQIgL/g+3i6jOPiaHh2Jf0vV59g1w1ZVxedrGWE4oMCPBAF7FRBMoQsSUFwHhpknj/fHQqgQVPw==' (length=88)
string 'Mw0AlrcC5v3v6swEg+wT+uTHf8MNrxdprD9GCpOe/45xedrGWE4oMCPBAF7FRBMoQsSUFwHhpknj/fHQqgQVPw==' (length=88)

Notice how the string all end in the same set of characters?

Is this supposed to happen?

I would have thought that because the data is different and I am creating a new initialisation vector each time it would have resulted in a totally different output.

Is there another, good, algorithm that would create totally different output given a small change in the data?

http://codepad.viper-7.com/jVqpUU

Was it helpful?

Solution

The similar result is due to the ECB cipher block mode where each block is encrypted separately. Use a different mode like CBC instead and the result of previous blocks will also influence later block encryptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top