简体   繁体   English

是什么会导致PHP mcrypt破坏数据?

[英]What could cause PHP mcrypt to mangle data?

I'm using an mcrypt function to encrypt a block of text using TripleDES. 我正在使用mcrypt函数使用TripleDES加密文本块。 90% of the time it works as it should and I can decrypt just fine. 90%的时间可以正常工作,我可以解密。 The other 10% though I just cannot decrypt it at all - as if I have the wrong key or the data is mangled. 其余10%的人虽然根本无法解密,仿佛我使用了错误的密钥或数据被损坏。

The function is as follows: 功能如下:

function enc($text, $key, $iv) {
  $text_num = str_split($text, 8);
  $text_num = 8 - strlen($text_num[count($text_num)-1]);

  for ($i=0; $i < $text_num; $i++) {
    $text = $text . chr($text_num);
  }

  $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
  mcrypt_generic_init($cipher, $key, $iv);
  $decrypted = mcrypt_generic($cipher, $text);
  mcrypt_generic_deinit($cipher);
  return base64_encode($decrypted);
}

They key & IV are the correct lengths (24/8 respectively) and never change. 它们的键和IV是正确的长度(分别为24/8),永远不变。 Like I said, it runs the exact same code on everything but only 10% fail in this way. 就像我说的,它在所有内容上运行完全相同的代码,但是只有10%的代码以这种方式失败。

Is there anything I could be passing in $text that is causing this? 我可能在$text中传递任何导致这种情况的信息吗? Does it not like certain character sets? 它不喜欢某些字符集吗? Or can this happen due to low memory/some other server condition? 还是由于内存不足/某些其他服务器状况而发生这种情况?

Any help pinning this down would be much appreciated. 任何帮助解决此问题的方法将不胜感激。 Thanks! 谢谢!

(Copied from comment) 3DES has a 64 bit blocksize (8 bytes). (从注释中复制)3DES具有64位块大小(8字节)。 You need to look at the exact size of the plaintext to check at it in terms of number of blocks. 您需要查看纯文本的确切大小,以便根据块数进行检查。 Look in the documentation for mention of padding, probably PKCS5 or possibly PKCS7. 在文档中查找有关填充的信息,可能是PKCS5或可能是PKCS7。 Is your "10%" failure actually 12.5% failure (1 in 8)? 您的“ 10%”失败实际上是12.5%失败(八分之一)吗? That would tend to point to something relating to blocking. 那将倾向于指向与阻塞有关的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM