Python 中可以使用 hashlib 模块来进行 MD5 加密。

示例代码如下:

import hashlib

def md5_encrypt(string):
    # 创建 MD5 对象
    md5 = hashlib.md5()

    # 更新 MD5 对象,添加要加密的字符串
    md5.update(string.encode('utf-8'))

    # 获取加密后的字符串
    result = md5.hexdigest()

    return result

使用示例:

string = 'hello world!'
result = md5_encrypt(string)
print(result)

输出结果为:

5eb63bbbe01eeed093cb22bb8f5acdc3
python 如何使用 MD5加密字符串