php5.5.9

-----------------------
$output = "test  php !!"

$key = "abcd123456789";

$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $output, MCRYPT_MODE_CBC);

$output = bin2hex($output);

echo $output;

 
---------------------------------------------------
php5.6 之后,mcrypt_encrypt 必须要 带 第五个参数 $iv,  但是加密出来的结果也可以和5.5的行为一样
1. php5.6 中 key 必须要8,16,32个字符;(之前的只有15个字符,php5.5其实是自动填 \0 )
2. php5.6 中 iv 必须要 ; (之前的版本, php5.5应该是默认填 16个\0 (至于是8个, 还是16个,32个,这个是和前面的key的长度有关的!));
所有,基于以上的原因,便有了下面php5.6对应的代码:
--------------------------------------------------------
 

$output = "test  php !!"

$key = "abcd123456789"."\0\0\0";
$iv = str_repeat("\0", 16);
$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $output, MCRYPT_MODE_CBC, $iv);

$output = bin2hex($output);

echo $output;

-------------------------------------------

Found the answer in case anyone need

$ivSize = 8;
$iv = str_repeat("\0", $ivSize); $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC, $iv));

Pass a 5th parameter manually which the earlier version was doing on its own!

--------------------------------------------------------------

参考:https://stackoverflow.com/questions/30475946/mcrypt-encrypt-not-working-properly-on-php-5-6-9/30477958#30477958

Asked 4 years, 5 months ago
Viewed 6k times
5
1

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.

Artjom B.

54.6k1818 gold badges9191 silver badges171171 bronze badges
asked May 27 '15 at 7:32
Saritha Nair

9911 silver badge1111 bronze badges
 

3 Answers

4

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.

answered May 27 '15 at 9:10
Daniel W.

22.3k77 gold badges5757 silver badges100100 bronze badges
 
2

Found the answer in case anyone need

$ivSize = 8;
$iv = str_repeat("\0", $ivSize); $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC, $iv));

Pass a 5th parameter manually which the earlier version was doing on its own!

Artjom B.

54.6k1818 gold badges9191 silver badges171171 bronze badges
answered May 27 '15 at 8:52
Saritha Nair

9911 silver badge11

php5.6 的mcrypt_encrypt 函数可以和5.5.9的行为一样的更多相关文章

  1. Php5.3的lambda函数以及closure(闭包)

    从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...

  2. php5 中魔术方法函数有哪几个

    魔术函数:9.3 构造函数:__construct() 9.3.1 实例化对象时被调用. 9.3.2 在类中,构造函数是用来初始化对象的,利用构造函数,可以操作对象,并改变它的值. 9.3.3 当__ ...

  3. [调试日志]用php函数var_export把多维数组file_put_contents写入并打印到日志,以方便调试之多维数组,用php5中的var_export函数示例,顺带介绍http_build_query(转)

    一行解决写入日志: file_put_contents("/tmp/jack.txt", var_export($layReturnArr,TRUE),FILE_APPEND); ...

  4. PHP学习之[第07讲]PHP5.4 文件操作函数 之 图片计数器的实例

    1.filetype():输出文件类型: 2.stat():获取文件的基本属性的数组: 3.clearstatcache().is_executable().isDir().idFile().scan ...

  5. mysql_connect() php7不支持,php5.5可以,是废弃函数

    天用了PHP7,发现和PHP5变化还挺大的,最大的就是MySQL的连接库变了. PHP5中使用mysql_connect()函数进行连接,但实际上,PHP5.5开始,MySQL就不推荐使用了,属于废弃 ...

  6. php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容

    最近,在用file_get_contents函数来取得文本的内容的时候,出现了一个情况(如下),苦思冥想了n久,不得其解,最后,果然还是得靠百度啊..... 百度到一个解释,下面是原文: PHP5中的 ...

  7. (转载)函数:mysqli_query和mysql_query有何区别?

    (转载)http://wzan315.blog.163.com/blog/static/37192636201241732045299/ Mysqli.dll是一个允许以对象的方式或者过程操作数据库的 ...

  8. PHP源码阅读(一):str_split函数

    注:源码版本:php5.6.33. 函数简介 str_split 原型: array str_split ( string $string [, int $split_length = 1 ] ) 说 ...

  9. [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...

    背景: 下面是获取文件的行数的方法: 一个文件如果知道有几行的话,就可以控制获取一定的行数的数据,然后放入数据库.这样不管的读取大文件的性能,还是写入数据库的性能,都能得到很大的提高了. 下面是获取文 ...

随机推荐

  1. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  2. DB2执行计划分析

    多表连接的三种方式详解 hash join.merge join. nested loop 项目中的SQL执行效率太低,就用执行计划看一下执行SQL,看不懂,百度一下,纪录下来: 大多数人从来没有听说 ...

  3. sql数据库查询相关操作,SQL的应用——SQL多表连查、子查询、多行子查询

    ? 1 **SQL多表连查** ? 1 2 3 4 5 6 7 8 --查询员工和部门信息 select * from emp e,dept d where e.deptno=d.deptno --查 ...

  4. 【转】Fuel 9.0安装Openstack网络验证失败解决

    原文链接:https://blog.csdn.net/wiborgite/article/details/52983575 故障现象: 网络验证失败,报错信息如下: Repo availability ...

  5. Tomcat详解|乐字节

    大家好,欢迎来到乐字节小乐的Java技术分享园地.这次给大家分享的是Tomcat   一. 什么是 Tomcat Tomcat 是一个符合 JavaEE WEB 标准的最小的 WEB 容器,所有的 J ...

  6. CPU使用率过高问题定位

    (1)top 命令 ->查询出CPU使用率最高的 PID编号. (2)top -H p PID编号 ->能查询出所有线程的CPU使用率的列表(线程编号也在PID列). (3)jstack ...

  7. Apache Kafka主题 - 架构和分区

    1.卡夫卡话题 在这篇Kafka文章中,我们将学习Kafka主题与Kafka Architecture的整体概念.Kafka中的体系结构包括复制,故障转移以及并行处理.此外,我们还将看到创建Kafka ...

  8. 解决sublime text3运行PyQt5代码不能显示窗口的问题

    如题,在sublime中写了GUI代码,Ctrl+B能运行,但是就是不能显示窗口. 解决办法:  找到路径C:\Users\superlee\AppData\Roaming\Sublime Text ...

  9. werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you mean 'user.index' instead?

    werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you mean 'user.index' ins ...

  10. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...