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

  • 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所有方向的学习路线(新版)**  

这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

最近我才对这些路线做了一下新的更新,知识体系更全面了。



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



**(2)Python学习视频**



包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

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



**(3)100多个练手项目**

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

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




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

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

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3DES (Triple Data Encryption Standard) 是一种对称加密算法,它使用三个不同的密钥进行加密。下面是一个使用 Python 实现 3DES 加密解密算法的示例代码: ```python from Crypto.Cipher import DES3 import base64 def encrypt_3des(key, data): # 将密钥转换为字节类型 key = key.encode('utf-8') # 创建 3DES 加密器 cipher = DES3.new(key, DES3.MODE_ECB) # 将数据转换为字节类型 data = data.encode('utf-8') # 使用 3DES 加密器加密数据 encrypted_data = cipher.encrypt(data) # 将加密后的数据进行 base64 编码后返回 return base64.b64encode(encrypted_data).decode('utf-8') def decrypt_3des(key, encrypted_data): # 将密钥转换为字节类型 key = key.encode('utf-8') # 创建 3DES 解密器 cipher = DES3.new(key, DES3.MODE_ECB) # 将加密后的数据进行 base64 解码 encrypted_data = base64.b64decode(encrypted_data) # 使用 3DES 解密器解密数据 decrypted_data = cipher.decrypt(encrypted_data) # 将解密后的数据转换为字符串类型并返回 return decrypted_data.decode('utf-8') ``` 使用示例: ```python key = '12345678901234567890123456789012' data = 'Hello, World!' encrypted_data = encrypt_3des(key, data) print('加密后的数据:', encrypted_data) decrypted_data = decrypt_3des(key, encrypted_data) print('解密后的数据:', decrypted_data) ``` 输出结果: ``` 加密后的数据: uF4LwX8JqfJbdSFV8iWj8Q== 解密后的数据: Hello, World! ``` 注意:以上代码使用了 PyCryptodome 库来实现 3DES 加密解密,需要先安装该库。可以使用以下命令来安装: ``` pip install pycryptodome ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值