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. SQL Server跨服务器操作数据库

    今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...

  2. Vue利用搜狐获取公网ip地址

    在index.html中添加代码: <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script> ...

  3. vue中$router以及$route的使用

    路由基本概念 route,它是一条路由. { path: '/home', component: Home } routes,是一组路由. const routes = [ { path: '/hom ...

  4. Java中遍历Map对象的4种方法

    java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等). HashMap<Inte ...

  5. Inno Setup 检测已安装的.NET Framework 版本

    翻译自:http://kynosarges.org/DotNetVersion.html 由 Jordan Russell 写的 Inno Setup 是一个伟大的安装脚本程序,但缺乏一个内置的函数来 ...

  6. 神奇的print

    一:多看看 1. #大小写转换 ,有大写的 全转化为大写 s = 'fds Kkg' print(s.swapcase()) #下划线等各种插入 s = 'fdsfkg' print('_'.join ...

  7. 使用 Angular RouteReuseStrategy 缓存(路由)组件

    使用 Angular RouteReuseStrategy 缓存组件 Cache components with Angular RouteReuseStrategy RouteReuseStrate ...

  8. Numpy学习笔记(上篇)

    目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...

  9. DevOps 什么是 CI/CD?

    CI/CD 是一种通过在应用开发阶段引入自动化来频繁向客户交付应用的方法.CI/CD 的核心概念是持续集成.持续交付和持续部署.作为一个面向开发和运营团队的解决方案,CI/CD 主要针对在集成新代码时 ...

  10. 阿里云ECS服务器将默认的Ubuntu系统改成桌面版

    以Ubuntu14.04 64位 为例 1.用自己PC登录阿里云,停止正在运行的实例 2.重装系统 更换系统盘->选择"公共镜像".Ubuntu. Ubuntu14.04 6 ...