2023-03-02 18:07:09 +08:00

25 lines
616 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from Crypto.Cipher import DES
key = b'abcdefgh' # 密钥 8位或16位,必须为bytes
def pad(text):
# 如果text不是8的倍数【加密文本text必须为8的倍数补足为8的倍数
while len(text) % 8 != 0:
text += ' '
return text
# 加密方法
def encrypt(key, text):
des = DES.new(key, DES.MODE_ECB) # 创建DES实例
padded_text = pad(text)
encrypted_text = des.encrypt(padded_text.encode('utf-8'))
return encrypted_text
# 解密方法
def decrypt(key, text):
des = DES.new(key, DES.MODE_ECB)
plain_text = des.decrypt(text).decode().rstrip(' ')