简体   繁体   中英

DES ECB encryption with PHP

https://www.tools4noobs.com/online_tools/encrypt/ gives "a67a318c98a0307502ba81caade2f3a9" as a DES ECB result for the key "1234567890abcdef" and payload "encrypt this".

The PHP code

echo bin2hex(mcrypt_encrypt(
    MCRYPT_DES,
    hex2bin("1234567890abcdef"),
    "encrypt this",
    MCRYPT_MODE_ECB)) . "\n";

prints out "1a29ee87f2ad67644ff28450c676a664".

What's wrong with the code?

The noobs4tools website strips out the hex2bin function and truncates the key length to 8 characters(as Yoshi stated in comments).

With a keysize of 12345678 the output of both the website and the PHP code is consistent.

The DES keysize is stated in the manual as being 56 bits. Read below some useful background on DES specific keysizes.

How should I create my DES key? Why is an 7-character string not enough?

Key Used by the noobs4tools website:

"12345678"

Key Used by your code:

 hex2bin("1234567890abcdef"); // 4Vx����

This difference then gives you the different outputs.

So the website does not translate the key into any other number- or data- form. It expects you to provide an already correctly formatted value in the page script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM