You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
505/222.py

30 lines
1.1 KiB

def message_decrypt():
decrypted_lines = ""
with open('sandbox_sender/desPassword.txt', 'r') as des_password_gotcha:
des_pwd = des_pwd_decrypt()
# 读取加密的消息(十六进制格式)
with open('sent/encrypted_msg.txt', 'r') as encrypted_file:
encrypted_lines = encrypted_file.read()
# 将十六进制字符串转换为二进制数据
encrypted_data = binascii.unhexlify(encrypted_lines)
# 创建 DES 解密器
des_cipher = DES.new(des_pwd, DES.MODE_ECB) # 使用 ECB 模式
# 解密文本
decrypted_message = des_cipher.decrypt(encrypted_data).decode()
# 移除 PKCS#7 填充
padding_length = ord(decrypted_message[-1]) # 填充长度是最后一个字符的 ASCII 值
decrypted_message = decrypted_message[:-padding_length] # 去除填充
# 打印解密后的消息
print("Decrypted Message:")
print(decrypted_message)
# 可选择将解密后的消息保存到文件
with open('inbox/decrypted_msg.txt', 'w') as decrypted_file:
decrypted_file.write(decrypted_message)
print("Decrypted message saved to 'decrypted_msg.txt'.")