php5.6 的mcrypt_encrypt 函数可以和5.5.9的行为一样
php5.5.9
-----------------------
$output = "test php !!"
$key = "abcd123456789";
$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $output, MCRYPT_MODE_CBC);
$output = bin2hex($output);
echo $output;
$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!
--------------------------------------------------------------
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.
- While I agree that a random IV should be used, it may not be applicable to the OPs case, because they already have a library full of ciphertexts which must be compatible. – Artjom B. May 27 '15 at 9:13
- @ArtjomB. good point, to ensure decryption of "legacy" ciphertexts, the "old" iv must be used.. – Daniel W. May 27 '15 at 9:14
- 1
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!
php5.6 的mcrypt_encrypt 函数可以和5.5.9的行为一样的更多相关文章
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- php5 中魔术方法函数有哪几个
魔术函数:9.3 构造函数:__construct() 9.3.1 实例化对象时被调用. 9.3.2 在类中,构造函数是用来初始化对象的,利用构造函数,可以操作对象,并改变它的值. 9.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); ...
- PHP学习之[第07讲]PHP5.4 文件操作函数 之 图片计数器的实例
1.filetype():输出文件类型: 2.stat():获取文件的基本属性的数组: 3.clearstatcache().is_executable().isDir().idFile().scan ...
- mysql_connect() php7不支持,php5.5可以,是废弃函数
天用了PHP7,发现和PHP5变化还挺大的,最大的就是MySQL的连接库变了. PHP5中使用mysql_connect()函数进行连接,但实际上,PHP5.5开始,MySQL就不推荐使用了,属于废弃 ...
- php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容
最近,在用file_get_contents函数来取得文本的内容的时候,出现了一个情况(如下),苦思冥想了n久,不得其解,最后,果然还是得靠百度啊..... 百度到一个解释,下面是原文: PHP5中的 ...
- (转载)函数:mysqli_query和mysql_query有何区别?
(转载)http://wzan315.blog.163.com/blog/static/37192636201241732045299/ Mysqli.dll是一个允许以对象的方式或者过程操作数据库的 ...
- PHP源码阅读(一):str_split函数
注:源码版本:php5.6.33. 函数简介 str_split 原型: array str_split ( string $string [, int $split_length = 1 ] ) 说 ...
- [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
背景: 下面是获取文件的行数的方法: 一个文件如果知道有几行的话,就可以控制获取一定的行数的数据,然后放入数据库.这样不管的读取大文件的性能,还是写入数据库的性能,都能得到很大的提高了. 下面是获取文 ...
随机推荐
- idea的groovy设置
idea的groovy设置 groovy使用的是java的jdk 新建groovy项目,第一次新建的时候,Groovy library是空的,需要点击右边的Create...新建. 但是由于brew ...
- Bat批处理之for/f详解
转自:https://www.cnblogs.com/zhangq/p/3988697.html 含有/F的for格式: FOR /F ["options"] %%i IN (fi ...
- swool教程链接汇总
参考地址: swoole教程第一节:进程管理模块(Process)-上 swoole教程第二节:基础的通讯实现-server篇-1 W3Cschool的swoole的系统教程 csdn网站swoole ...
- 【视频开发】 十全大补:CxImage图像处理类库
十全大补:CxImage图像处理类库 转载IT168 CxImage是一个可以用于MFC 的C++图像处理类库类,它可以打开,保存,显示,转换各种常见格式的图像文件,比如BMP, JP ...
- 【计算机视觉】目标检测中的指标衡量Recall与Precision
[计算机视觉]目标检测中的指标衡量Recall与Precision 标签(空格分隔): [图像处理] 说明:目标检测性能指标Recall与Precision的理解. Recall与Precision ...
- idea关闭sonarLint自动扫描
手动运行SonarLint 停止SonarLint自动检测代码之后,可以使用Ctrl+Shift+S手动运行SonarLint检查代码
- Java反射桥接方法解析
在阅读mybaits源码的反射模块时,看到了如下的一段代码: /** * 添加 Method 数组到 uniqueMethods * @param uniqueMethods * @param met ...
- Aix6.1下su命令不能切换环境变量的问题
su是Aix的通用命令,和linux系统下一样,用来切换当前用户.切换用户执行命令使用如下命令: su - $user -c "$command" su -命令区别于su的地方是它 ...
- Testbench编写技巧
一.基本架构(常用模板) `timescale 1ns/1ps //时间精度 `define Clock //时钟周期 module my_design_tb; //================= ...
- Java date日期类型,结束日期减去开始日期求两者时间差,精确到秒
/** * @Author: * @Description: * @Date: 2019/4/10 19:01 * @Modified By: */ @Slf4j public class DateU ...
