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!
最新文章
- 程序猿是如何解决SQLServer占CPU100%的
- 解读ASP.NET 5 &; MVC6系列(2):初识项目
- LuaSocket http笔记
- FMDB 使用方法
- JavaScript学习基础部分
- 怎么在官网下载jstl【配图详解】
- Eclipse 打开当前文件所在的文件夹
- 【编程技巧】NSTimer类的使用
- php代码一样,编码不同报错
- 4 sum
- Java 合并、拆分PDF文档
- 主席树+树链剖分——南昌邀请赛Distance on the tree
- No fallback instance of type class found for feign client user-service(转)
- RobotFramework - AppiumLibrary 之关键字Open Application使用
- 为什么要使用mybaits
- Mybatis中输入输出映射和动态Sql
- post请求的header
- Python---字典常用方法总结
- 虚拟机扩容mac
- 【ORA错误大全】 ORA-19527
热门文章
- Ingress 访问日志分析与监控
- 【VS开发】【视频开发】利用ffmpeg+opencv实现画中画
- flask,scrapy,django信号
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
- 【LeetCode】删除链表的倒数第N个节点【双指针法】
- pom.xml文件导入了坐标,也没有报错,为什么还是没有相关的jar包的?
- django.db.utils.ProgrammingError: 1146 解决办法
- pythony--运算符
- 网页中插入Flash动画(.swf)代码和常用参数设置
- springboot 实时监控 spring-boot-starter-actuator 包