Home > Backend Development > PHP Tutorial > des encryption and decryption algorithm - How can I make php code: 62837 << -16 get the same result in windows and linux?

des encryption and decryption algorithm - How can I make php code: 62837 << -16 get the same result in windows and linux?

WBOY
Release: 2016-07-06 13:52:46
Original
1017 people have browsed it

Perform the des encryption operation and find that the results obtained in windows and linux are different. It is found that this operation is included in the createkey process, and it is different from here on.

The network query revealed that it was caused by int overflow, which is correct under Windows. It is recommended to operate through the gmp extension, but I have not found the specific usage. Please give me some advice

win’s result: -176881664
lin’s result: -759700962142060544

@eechen

Reply content:

Perform the des encryption operation and find that the results obtained in windows and linux are different. It is found that this operation is included in the createkey process, and it is different from here on.

The network query revealed that it was caused by int overflow, which is correct under Windows. It is recommended to operate through the gmp extension, but I have not found the specific usage. Please give me some advice

win’s result: -176881664
lin’s result: -759700962142060544

@eechen

Congratulations on falling into a big pit of php.

If a given number exceeds the range of integer, it will be interpreted as float. Similarly, if the result of the operation exceeds the range of integer, float will also be returned.

It is this problem that leads to all kinds of weird problems.

PHP does not support unsigned integers.

Quote php original text

However, this problem occurs because the lengths of the long types of your two systems (the int type of PHP are both long types in the kernel) are inconsistent (because the number of bits in the systems is different).
So, if you want to solve this problem, you must manually process int to uint.

<code class="php">$flag=$int&0x80000000;
//取出符号位,判断是否是负数,溢出int范围,但没溢出uint范围时,符号位为1,即为负数
if($flag){
    $int ^= 0xffffffff;
    $int += 1;
}
</code>
Copy after login

BTW, the latest php7 fixes this problem, but a negative number cannot be shifted in php.

I don’t know where this code comes from. The code for shifting negative numbers left and right is performed by the CPU, so it completely relies on the machine and has little to do with the system. Let’s see if we can correct this code, or The only way is to use other methods instead of shifting negative numbers left and right.

So starting from php7, if the displacement is a negative number, an error will be reported directly...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template