Python 和 PHP 之间的 MD5 不匹配

发布于 2025-01-09 08:47:55 字数 924 浏览 0 评论 0原文

我正在尝试比较 PHP 和 Python 之间的 MD5 字符串,我们的服务器在 PHP 客户端上运行良好,但是当我们尝试在 python 中执行相同操作时,我们总是从服务器得到无效响应。

我在Python中有以下代码

import hashlib
keyString = '96f6e3a1c4748b81e41ac58dcf6ecfa0'
decodeString = ''
length = len(keyString)
for i in range(0, length, 2):
   subString1 = keyString[i:(i + 2)]
   decodeString += chr(int(subString1, 16))
print(hashlib.md5(decodeString.encode("utf-8")).hexdigest())

Produces: 5a9536a1490714cb77a02080f902be4c

现在,在PHP中具有相同的概念:

$serverRandom = "96f6e3a1c4748b81e41ac58dcf6ecfa0";
$length = strlen($serverRandom);
$server_rand_code = '';
for($i = 0; $i < $length; $i += 2)
  {
    $server_rand_code .= chr(hexdec(substr($serverRandom, $i, 2)));
  }
echo 'SERVER CODE: '.md5($server_rand_code).'<br/>';

Produces: b761f889707191e6b96954c0da4800ee

我尝试检查编码,但没有运气,两个MD5输出不完全匹配,有什么帮助吗?

I am trying to compare the MD5 string between PHP and Python, the server we have is working fine with PHP clients, but when we tried to do the same in python, we always get an invalid response from the server.

I have the following piece of code In Python

import hashlib
keyString = '96f6e3a1c4748b81e41ac58dcf6ecfa0'
decodeString = ''
length = len(keyString)
for i in range(0, length, 2):
   subString1 = keyString[i:(i + 2)]
   decodeString += chr(int(subString1, 16))
print(hashlib.md5(decodeString.encode("utf-8")).hexdigest())

Produces: 5a9536a1490714cb77a02080f902be4c

now, the same concept in PHP:

$serverRandom = "96f6e3a1c4748b81e41ac58dcf6ecfa0";
$length = strlen($serverRandom);
$server_rand_code = '';
for($i = 0; $i < $length; $i += 2)
  {
    $server_rand_code .= chr(hexdec(substr($serverRandom, $i, 2)));
  }
echo 'SERVER CODE: '.md5($server_rand_code).'<br/>';

Produces: b761f889707191e6b96954c0da4800ee

I tried checking the encoding, but no luck, the two MD5 output don't match at all, any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-16 08:47:55

看起来您生成字节字符串的方法不正确,因此 hashlib.md5 的输入是错误的:

print(decodeString.encode('utf-8'))
# b'\xc2\x96\xc3\xb6\xc3\xa3\xc2\xa1\xc3\x84t\xc2\x8b\xc2\x81\xc3\xa4\x1a\xc3\x85\xc2\x8d\xc3\x8fn\xc3\x8f\xc2\xa0'

将字符串解释为十六进制字节字符串的最简单方法是使用 binascii.unhexlifybytes.fromhex

import binascii

decodeString  = binascii.unhexlify(keyString)
decodeString2 = bytes.fromhex(keyString)

print(decodeString)
# b'\x96\xf6\xe3\xa1\xc4t\x8b\x81\xe4\x1a\xc5\x8d\xcfn\xcf\xa0'

print(decodeString == decodeString2)
# True

您现在可以直接在 hashlib.md5 中使用生成的 bytes 对象:

import hashlib

result = hashlib.md5(decodeString)
print(result.hexdigest())
# 'b761f889707191e6b96954c0da4800ee'

Looks like your method of generating the byte string is incorrect, so the input to hashlib.md5 is wrong:

print(decodeString.encode('utf-8'))
# b'\xc2\x96\xc3\xb6\xc3\xa3\xc2\xa1\xc3\x84t\xc2\x8b\xc2\x81\xc3\xa4\x1a\xc3\x85\xc2\x8d\xc3\x8fn\xc3\x8f\xc2\xa0'

The easiest way to interpret the string as a hex string of bytes is to use binascii.unhexlify, or bytes.fromhex:

import binascii

decodeString  = binascii.unhexlify(keyString)
decodeString2 = bytes.fromhex(keyString)

print(decodeString)
# b'\x96\xf6\xe3\xa1\xc4t\x8b\x81\xe4\x1a\xc5\x8d\xcfn\xcf\xa0'

print(decodeString == decodeString2)
# True

You can now directly use the resulting bytes object in hashlib.md5:

import hashlib

result = hashlib.md5(decodeString)
print(result.hexdigest())
# 'b761f889707191e6b96954c0da4800ee'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文