2013-04-10 59 views
0

運行此:PHP的mcrypt相同尾部字符

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)))); 

其結果:

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) 

注意如何在字符串的所有最終以同樣的字符集?

這是應該發生的嗎?

我會認爲,因爲數據是不同的,我每創建一個新的初始化向量它會導致完全不同的輸出。

是否有另一個很好的算法會在數據發生微小變化時創建完全不同的輸出?

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

+1

http://codepad.viper-7.com/zD8ogl – 2013-04-10 05:18:09

回答

1

類似的結果是由於,其中每個塊單獨加密的ECB cipher block mode。改用CBC等不同的模式,之前的模塊結果也會影響後面的模塊加密。

+0

每種方法的優缺點是什麼? – Petah 2013-04-10 06:07:35

+0

@Petah你已經認識到了自己的弱點:相同的輸入被加密相同。有關更多信息,請閱讀鏈接文章。 – Gumbo 2013-04-10 16:17:23