mcrypt_encrypt在PHP 5.6.9上无法正常工作 [英] mcrypt_encrypt not working properly on PHP 5.6.9

查看:507
本文介绍了mcrypt_encrypt在PHP 5.6.9上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数索引()
{
echo $ this-> encryptText_3des('TEST','JHHKJH9879');
}

函数encryptText_3des($ plainText,$ key){
$ key = hash(md5,$ key,TRUE); ($ x = 0; $ x< 8; $ x ++){
$ key = $ key.substr($ key,$ x,1)

}
$ padded = $ this-> pkcs5_pad($ plainText,
mcrypt_get_block_size(MCRYPT_3DES,MCRYPT_MODE_CBC));
$ encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES,$ key,$ padded,MCRYPT_MODE_CBC));
返回$加密;
}

函数pkcs5_pad($ text,$ blocksize)
{
$ pad = $ blocksize - (strlen($ text)%$ blocksize);
返回$文本。 str_repeat(chr($ pad),$ pad);
}

加密正在发生。但在5.6.9中,在PHP文件mcrypt_encrypt,他们提到


无效的密钥和iv大小不再被接受。如果输入无效,mcrypt_encrypt()将抛出一个警告并返回FALSE。以前的键和IV填充'\0'字节到下一个有效的大小。


我将如何修改我当前的代码第五个参数而不改变加密算法?



我尝试了

  $ iv_size = mcrypt_get_iv_size(MCRYPT_3DES,MCRYPT_MODE_CBC); 
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND);

并给出$ iv作为第五个参数。



但它没有奏效。加密与以前的加密不同。

解决方案

不要模拟旧的PHP版本,用于初始化IV的弱行为。 >

使用 mcrypt_create_iv()



他们删除了一个原因的自动零字节iv


I have the following code which worked fine on PHP 5.5.9.

function index()
{
    echo $this->encryptText_3des('TEST','JHHKJH9879');
}

function encryptText_3des($plainText, $key) {
    $key = hash("md5", $key, TRUE); 
    for ($x=0;$x<8;$x++) {
        $key = $key.substr($key, $x, 1);
    }
    $padded = $this->pkcs5_pad($plainText,
    mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
    return $encrypted;
}

function pkcs5_pad ($text, $blocksize)  
{
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

The encryption was happening fine.But in 5.6.9, the in the PHP doc of mcrypt_encrypt, they mention that

Invalid key and iv sizes are no longer accepted. mcrypt_encrypt() will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '\0' bytes to the next valid size.

How will I modify my current code with the fifth parameter without altering the encryption algorithm?

I tried

$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

and given $iv as fifth parameter.

But it didn't work out. The encryption was different from the earlier one.

解决方案

Don't emulate old PHP versions weak behaviour for initializing IV.

Use mcrypt_create_iv().

They removed the auto zero-byte iv for a reason.

这篇关于mcrypt_encrypt在PHP 5.6.9上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆