简体   繁体   English

在Python中计算Hex的MD5

[英]Calculating MD5 of Hex in Python

I always fail with the decrypting of a RC4 encrypted object. 我总是因解密RC4加密对象而失败。 My key has to be the MD5-Hash of this hex string: 我的密钥必须是这个十六进制字符串的MD5-Hash:

00 00 00 01 3e 2a 5b 71 00 00 03 a0

What i tried was to convert that hex string to ascii and calculate afterwards the MD5 hash of it, but it seems like my key is always wrong. 我尝试的是将该十六进制字符串转换为ascii,然后计算它的MD5哈希,但似乎我的密钥总是错误的。 I think there is a problem because some of the hex values are control characters, but how is then the correct way to calculate the MD5 of this hex string ? 我认为有一个问题,因为一些十六进制值是控制字符,但是如何计算这个十六进制字符串的MD5的正确方法呢? What i thought of was something like this: 我想到的是这样的:

from Crypto.Cipher import ARC4
from Crypto.Hash import MD5


def hexToAscii(hex_string):
    return ''.join([chr(int(''.join(c), 16)) for c in zip(hex_string[0::2],hex_string[1::2])])

def main():
    hex_string = '000000013e2a5b71000003a0'

    # Key for Decryption
    myKey = MD5.new(hexToAscii(hex_string)).hexdigest()

    print 'hexToAscii(hex_string): %s' % hexToAscii(hex_string)

    #open('myfile','wb').write(ARC4.new(hexToAscii(myKey)).decrypt(hexToAscii(CIPHER_TEXT)))

if __name__ == '__main__':
    main()

The main function prints hexToAscii(hex_string) instead of myKey . main函数打印hexToAscii(hex_string)而不是myKey

BTW, you'd better to use binascii.unhexlify instead of hexToAscii . 顺便说一句,你最好使用binascii.unhexlify而不是hexToAscii And you can use hashlib module to calculate md5. 并且您可以使用hashlib模块来计算md5。

>>> import hashlib
>>> import binascii
>>> hex_string = '000000013e2a5b71000003a0'
>>> hashlib.md5(binascii.unhexlify(hex_string)).hexdigest()
'6afebf522c531575e96d6814be816c7c'

Use hashlib and binascii from Python standard lib, no conversion to ASCII involved: 使用Python标准库中的hashlibbinascii ,不涉及到涉及的ASCII转换:

import binascii
import hashlib

base = binascii.unhexlify("000000013e2a5b71000003a0")
key = hashlib.md5(base).digest()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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