doupu3635 2012-09-25 13:39
浏览 115
已采纳

为加密密码添加了空格?

I have implemented functions in my login model for decrypting/encrypting a users password (which in a encrypted version gets stored in a cookie). All works fine except when I restart the browser and try to login with the password through the cookies.

The parser tells me it's the wrong password even though I can see that it's the correct one when I echo it out. Therefore I tried a "strlen" to see how many characters the password has, and it says 32 (!). The only thing I can think of is that (in this case) 28 white spaces has been added, which cannot be seen with an echo.

I would really appreciate if someone can tell me what's going on and how to fix it?

function decrypt($encrypedText) {
    $key = "The secret key is";
    $decryptedText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encrypedText), MCRYPT_MODE_ECB);

    echo $decryptedText;            //  "abcd" <- what I put in
    echo strlen($decryptedText);    //  32 (?)

    return $decryptedText;
}
  • 写回答

2条回答 默认 最新

  • dongmeng0317 2012-09-25 14:00
    关注

    That's because 32 bytes is the block size of Rijndael 256 (i.e. 32 = 256 / 8) and the decrypted data is padded with '\0' to match that length.

    To correct this you can remove those characters like so:

    return rtrim($decryptedText, '\0');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 quartus II 9.0闪退问题怎么解决啊,急
  • ¥20 求自动化运维语料数据集
  • ¥30 广告检测流量作弊案例 IDEA运行代码报错 连接不上metastore 检测了环境配置没有问题 请求远程解决加VX问细节问题 不加的不回复
  • ¥15 matlab图像融合代码被嫌弃太简单,求改进。第一步改成直接读取三张图片,不读取文件夹
  • ¥20 微处理器原理与应用(私有偿)
  • ¥50 8051单片机关于ADC0809的应用
  • ¥15 有没有能拿来练练手写完发给我
  • ¥15 禁止修改windows系统时间
  • ¥50 kinect连接win11笔电导致音视频设备消失
  • ¥15 python线性查找题