md5 Python代码
时间: 2023-11-05 09:07:45 浏览: 38
可以使用Python内置的hashlib库来生成MD5摘要。以下是一个简单的示例代码:
```python
import hashlib
string = "Hello world"
hash_object = hashlib.md5(string.encode())
md5_hash = hash_object.hexdigest()
print("MD5 hash of '%s': %s" % (string, md5_hash))
```
输出结果:
```
MD5 hash of 'Hello world': ed076287532e86365e841e92bfc50d8c
```
相关问题
pythonmd5解密代码
以下是 Python 中使用 hashlib 模块进行 MD5 解密的代码示例:
```python
import hashlib
def md5_decrypt(hash_str):
# 创建一个 MD5 对象
md5_obj = hashlib.md5()
# 将字符串转换为二进制并更新 MD5 对象
md5_obj.update(hash_str.encode())
# 获取 MD5 的十六进制表示
decrypted_str = md5_obj.hexdigest()
return decrypted_str
```
该函数接受一个字符串参数 `hash_str`,将其转换为二进制并使用 `hashlib.md5()` 创建一个 MD5 对象。接着,使用 `update()` 方法更新 MD5 对象,并使用 `hexdigest()` 方法获取其十六进制表示,最终返回解密后的字符串。
python md5解密代码
MD5是一种哈希算法,通常不支持解密。但是可以使用暴力破解方法来找到与给定MD5哈希值匹配的原始值。以下是使用Python的示例代码:
```python
import hashlib
hash_value = input("Enter the MD5 hash value: ")
password_list = input("Enter the password file name: ")
try:
password_file = open(password_list, "r")
except:
print("File not found")
quit()
for password in password_file:
encoded_password = password.encode('utf-8')
hash_object = hashlib.md5(encoded_password.strip())
guess_hash = hash_object.hexdigest()
if guess_hash == hash_value:
print("Password found: ", password.strip())
quit()
print("Password not found in the list")
```
请注意,这个程序需要一个包含密码列表的文本文件作为输入,并尝试对每个密码进行MD5哈希,以查找与给定哈希值匹配的密码。请确保您有权访问这个文件并且只能用于合法目的。