doujiong2533 2012-11-21 12:30
浏览 88
已采纳

php mcrypt:为什么要在字符串输入上添加空格?

There's a mcrypt snippet on several webs:

https://stackoverflow.com/a/11538728/408872

http://www.binrand.com/post/3810303-mcrypt-md5-how-to-create-an-online-encryption-decryption-web-page.html

http://www.techbees.org/best-way-to-use-php-to-encrypt-and-decrypt/

$key = 'password to (en/de)crypt';
$string = ' string to be encrypted '; // note the spaces

$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

echo 'Encrypted:' . "
";
var_dump($encrypted);

echo "
";

echo 'Decrypted:' . "
";
var_dump($decrypted); // spaces are preserved

Anybody knows why extra spaces are introduced on $string?

  • 写回答

1条回答 默认 最新

  • douyuqing_12345 2012-11-21 12:37
    关注

    The sources you have provided all use nearly exactly the same code. I can only assume they stole the code from each other. So it's not several sites doing this, it was most likely a single site, and the others who didn't really understand what was going on simply inherited this.

    There is no good reason to add additional spaces to a string prior to encrypting it, it does not enhance the strength of the cryptography in any way, it doesn't make it "better".

    My guess would be that in these examples, it has been done to show that functions like trim do not affect the contents of an encrypted stream. (I personally don't understand why this needs to be proved - it seems entirely logical to me)

    If you're accepting a base64 encoded encrypted data (perhaps from a form submission), you will most likely trim the submitted data to remove any chaff the user left in there, I suspect someone somewhere was concerned that trimming the encrypted data would break the original plaintext.

    Edit:

    Note, if this string wasn't base64 encoded, it would be entirely possible to damage the ciphertext. Since the ciphertext would be represented in binary form, it is possible that it would have whitespace characters at the end, which could be trimmed off causing corruption.

    Edit 2:

    md5(key) for the key, and md5(md5(key)) for the initialisation vector are both terrible terrible ways to implement this.

    Firstly, AES 256 should have a 256 bit key. If you want to use a passphrase, then use hash('sha256', $passphrase, true). The final true makes it return the result as binary, not hex encoded, and this is important so you actually get as much entropy as possible in your key.

    Secondly, initialisation vectors should not be re-used, ever. md5(md5(key)) will always produce the same value, for the same key. This weakens your encryption considerably if an attacker manages to obtain several ciphertexts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路