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

最新文章

  1. 程序猿是如何解决SQLServer占CPU100%的
  2. 解读ASP.NET 5 &amp; MVC6系列(2):初识项目
  3. LuaSocket http笔记
  4. FMDB 使用方法
  5. JavaScript学习基础部分
  6. 怎么在官网下载jstl【配图详解】
  7. Eclipse 打开当前文件所在的文件夹
  8. 【编程技巧】NSTimer类的使用
  9. php代码一样,编码不同报错
  10. 4 sum
  11. Java 合并、拆分PDF文档
  12. 主席树+树链剖分——南昌邀请赛Distance on the tree
  13. No fallback instance of type class found for feign client user-service(转)
  14. RobotFramework - AppiumLibrary 之关键字Open Application使用
  15. 为什么要使用mybaits
  16. Mybatis中输入输出映射和动态Sql
  17. post请求的header
  18. Python---字典常用方法总结
  19. 虚拟机扩容mac
  20. 【ORA错误大全】 ORA-19527

热门文章

  1. Ingress 访问日志分析与监控
  2. 【VS开发】【视频开发】利用ffmpeg+opencv实现画中画
  3. flask,scrapy,django信号
  4. LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
  5. 【LeetCode】删除链表的倒数第N个节点【双指针法】
  6. pom.xml文件导入了坐标,也没有报错,为什么还是没有相关的jar包的?
  7. django.db.utils.ProgrammingError: 1146 解决办法
  8. pythony--运算符
  9. 网页中插入Flash动画(.swf)代码和常用参数设置
  10. springboot 实时监控 spring-boot-starter-actuator 包