python中cryptodome的DES

pycryptodome库

安装

linux: pip3 install pycryptodome

导入: import Crypto

windows: pip install pycryptodomex

导入: import Cryptodome

使用

DES加密

DES为对称加密, 需要一个密钥来加密和解码数据.

from Cryptodome.Cipher import DES
import binascii


# 这是密钥
key = b'abcdefgh'   # key需为8字节长度.
# 需要去生成一个DES对象
des = DES.new(key, DES.MODE_ECB)
# 需要加密的数据
text = 'python spider!'     # 被加密的数据需要为8字节的倍数.
text = text + (8 - (len(text) % 8)) * '='
print(text)
# 加密的过程
encrypto_text = des.encrypt(text.encode())
# encrypto_text = binascii.b2a_hex(encrypto_text)
print(encrypto_text)

decrrpto_text = des.decrypt(encrypto_text)
# decrrpto_text = binascii.b2a_hex(decrrpto_text)
print(decrrpto_text)

猜你喜欢

转载自blog.csdn.net/One_of_them/article/details/82049232
DES