Skip to content

Instantly share code, notes, and snippets.

@miodeqqq
Last active February 21, 2024 13:38
Show Gist options
  • Save miodeqqq/8e064f14d446348914f0e45818234a2e to your computer and use it in GitHub Desktop.
Save miodeqqq/8e064f14d446348914f0e45818234a2e to your computer and use it in GitHub Desktop.
Python MD5 decrypt.
# -*- coding: utf-8 -*-
import hashlib
import sys
import time
# Using: ./hash.py hashcode
# For example: ./hash.py 9743a66f914cc249efca164485a19c5c
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s Time: %0.3f s' % (f.func_name, float(time2 - time1))
return ret
return wrap
@timing
def decryptMD5(testHash):
s = []
while True:
m = hashlib.md5()
for c in s:
m.update(chr(c))
hash = m.hexdigest()
if hash == testHash:
return ''.join([chr(c) for c in s])
wrapped = True
for i in range(0, len(s)):
s[i] = (s[i] + 1) % 256
if s[i] != 0:
wrapped = False
break
if wrapped:
s.append(0)
print(decryptMD5(sys.argv[1]))
@islamgab
Copy link

islamgab commented Jul 7, 2019

it`s take long time
python2 md5_hash_decrypt.py 202cb962ac59075b964b07152d234b70
decryptMD5 Time: 10.695 s <<<<<<<<<<
123

@rootkit7628
Copy link

Can this code be running in python 3.8

@r4gePy
Copy link

r4gePy commented May 7, 2020

Yep, it don't work on Python 3.8

@ashishsme14
Copy link

ashishsme14 commented Dec 7, 2020

how we can get value of 6ad48a76bec847ede2ad2c328978bcfa , 64bd1e146726e9f8622756173ab27831

try to run above code not getting output also

@ashishsme14
Copy link

$python main.py
Traceback (most recent call last):
File "main.py", line 48, in
print(decryptMD5(sys.argv[1]))
IndexError: list index out of range

@ashishsme14
Copy link

ashishsme14 commented Dec 8, 2020

$python3 main.py
File "main.py", line 15
print '%s Time: %0.3f s' % (f.func_name, float(time2 - time1))
^
SyntaxError: invalid syntax

in Execute Python-3 Online (Python v3.6.2)

@Debetome
Copy link

Debetome commented Dec 20, 2021

Make it run in python 3 is actually pretty easy, you just need to wrap the print string in parenthesis and then correct the line 29 encoding the Unicode character in m.update(chr(c)) into bytes, it has to be m.update(chr(c).encode("utf-8")) instead, because the update() method in the md5 object expects byte characters, and finally in the decorator function timing(f): to get the function name, you need to use the __name__ attribute instead of func_name if it is even relevant to you to know the function name, if not, just remove %s at the beginning of the string in the print function as well as f.func_name, , if you only care about the time it took the script to decrypt the hash.

Though I suggest you should learn first some of python before even thinking on running anyone else's scripts, so you'll be able to see how the script works and even solve these problems by yourselves that are actually QUITE basic.

The next code works in python 3.10, so it should work in python 3.8 or 3.6 just fine ...

`

import hashlib
import sys
import time

def timing(f):
    def wrap(*args):
        time1 = time.time()
        ret = f(*args)
        time2 = time.time()
        print('%s Time: %0.3f s' % (f.__name__, float(time2 - time1)))
        return ret

    return wrap


@timing
def decryptMD5(testHash):
    s = []

    while True:
        m = hashlib.md5()
        for c in s:
            m.update(chr(c).encode("utf-8"))

        hash = m.hexdigest()
        if hash == testHash:
            return ''.join([chr(c) for c in s])

        wrapped = True
        for i in range(0, len(s)):
            s[i] = (s[i] + 1) % 256

            if s[i] != 0:
                wrapped = False
                break

        if wrapped:
            s.append(0)

def main() -> None:
    if len(sys.argv) != 2:
        print("[-] Please especify an hash to decrypt!")
        print(f"[*] Usage: python3 {sys.argv[0]} <MD5 HASH>")
        sys.exit(-1)

    print("[*] Decrypting hash!")
    print(decryptMD5(sys.argv[1]))
    print("[+] Hash decrypted! :)")

if __name__ == '__main__':
    main()

`

@rbonciu
Copy link

rbonciu commented Aug 30, 2023

Hi! Nice work! There is an way to add hint? Suppose we know part of the hash like 0192023a7bbd73250516f069df18b500 (admin123) and I know "min1". I think that maybe it is something more efficient and decreases the decryption time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment