«
python3对字符串进行md5加密
myluzh 发布于
阅读:1005
Python
# 由于MD5模块在python3中被移除
# 在python3中使用hashlib模块进行md5操作
import hashlib
# 输入待加密字符串
str = input('Please enter the string to be MD5 encrypted:\n')
# 创建md5对象
m = hashlib.md5()
# Tips
# 此处必须encode
# 若写法为m.update(str) 报错为: Unicode-objects must be encoded before hashing
# 因为python3里默认的str是unicode
# 或者 b = bytes(str, encoding='utf-8'),作用相同,都是encode为bytes
b = str.encode(encoding='utf-8')
m.update(b)
str_md5 = m.hexdigest()
#输出md5加密后的值
print('After MD5 encryption:\n' + str_md5)
python md5