# 2024年Python最新python实现对称加解密3DES算法_python 3des

129 篇文章 0 订阅
• Read a hex string from the console input. The string represents the plaintext bytes as a hex string.
• Read a hex string from the console input. The string represents the first key bytes as a hex string.
• Read a hex string from the console input. The string represents the second key bytes as a hex string.
• Read a hex string from the console input. The string represents the third key bytes as a hex string.
• Encrypt the plaintext with the three keys.
• Print the ciphertext bytes as a hex string.
• Decrypt the ciphertext with the three keys.
• Print the plaintext bytes after decryption as a hex string.
###### Example Input & Output

Input:

8787878787878787
133457799bbcdff1
0e329232ea6d0d73
133457799bbcdff1



Output:

e98a0b8e59b3eeb7
8787878787878787


###### solution code
from libdes import DES_Encrypt, DES_Decrypt

def validate\_des\_key(key: bytes) -> bool:
for keyByte in key:
binStr: str = "{0:0>8b}".format(keyByte)
if sum([1 if b == '1' else 0 for b in binStr]) % 2 == 0:
return False
return True

if __name__ == '\_\_main\_\_':
plaintextHex: str = input('plaintext:')
key1Hex: str = input('key1:')
if not validate_des_key(bytes.fromhex(key1Hex)):
raise Exception('Parity check failed on the key.')
key2Hex: str = input('key2:')
if not validate_des_key(bytes.fromhex(key2Hex)):
raise Exception('Parity check failed on the key.')
key3Hex: str = input('key3:')
if not validate_des_key(bytes.fromhex(key3Hex)):
raise Exception('Parity check failed on the key.')

ciphertext1: bytes = DES_Encrypt(
bytes.fromhex(plaintextHex),
bytes.fromhex(key1Hex),
)

ciphertext2: bytes = DES_Decrypt(
ciphertext1,
bytes.fromhex(key2Hex),
)

ciphertext3: bytes = DES_Encrypt(
ciphertext2,
bytes.fromhex(key3Hex),
)

print('ciphertext:', ciphertext3.hex())

plaintext3: bytes = DES_Decrypt(
ciphertext3,
bytes.fromhex(key3Hex),
)

plaintext2: bytes = DES_Encrypt(
plaintext3,

**（1）Python所有方向的学习路线（新版）**

![在这里插入图片描述](https://img-blog.csdnimg.cn/1f807758e039481fa866130abf71d796.png#pic_center)

**（2）Python学习视频**

**（3）100多个练手项目**

![在这里插入图片描述](https://img-blog.csdnimg.cn/f5aeb4050ab547cf90b1a028d1aacb1d.png#pic_center)

**网上学习资料一大堆，但如果学到的知识不成体系，遇到问题时只是浅尝辄止，不再深入研究，那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友，可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快，但一群人才能走的更远！不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人，都欢迎加入我们的的圈子（技术交流、学习资源、职场吐槽、大厂内推、面试辅导），让我们一起学习成长！**

• 9
点赞
• 1
收藏
觉得还不错? 一键收藏
• 0
评论
04-12 3431
06-30 591
04-26 330
05-29 724
04-12 263
10-27 5万+
05-21 4579
07-23 1072

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。