python实现对称加解密3DES算法

⭐本专栏主要用python实现密码学中的常用经典算法,例如Vigenere、3DES、RSA、ElGamal、Diffie-Hellman、RSA签名、ElGamal签名、HMAC、哈希算法、列移位、AES等等。
🔥文章和代码已归档至【Github仓库:cryptography-codebase】,需要的朋友们自取。或者公众号【AIShareLab】回复 密码学 也可获取。

Program : 3DES

In this program, you are required to implement the 3DES algorithm using the provided encrypt and decrypt function of DES. The encrypt and decrypt method of 3DES should also be pure functions, i.e. without side effects.

Your program does the following:

  • 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,
        bytes.fromhex(key2Hex),
    )

    plaintext1: bytes = DES_Decrypt(
        plaintext2,
        bytes.fromhex(key1Hex),
    )

    print('plaintext:', plaintext1.hex())


output
plaintext:8787878787878787
key1:133457799bbcdff1
key2:0e329232ea6d0d73
key3:133457799bbcdff1
ciphertext: e98a0b8e59b3eeb7
plaintext: 8787878787878787

进程已结束,退出代码为 0

  • 70
    点赞
  • 195
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
DES加解密算法是一种对称加密算法,它使用相同的密钥对数据进行加密和解密。在Python中,我们可以使用pycryptodome库来实现DES加解密算法。 首先,我们需要安装pycryptodome库。可以使用以下命令进行安装: ``` pip install pycryptodome ``` 接下来,我们可以使用以下代码来实现DES加解密算法: ```python from Crypto.Cipher import DES # 加密函数 def des_encrypt(key, text): # 将密钥调整为8字节长度 key = key[:8].encode('utf-8') # 创建DES对象并进行加密 des = DES.new(key, DES.MODE_ECB) encrypted_text = des.encrypt(text.encode('utf-8')) # 返回加密结果 return encrypted_text.hex() # 解密函数 def des_decrypt(key, encrypted_text): # 将密钥调整为8字节长度 key = key[:8].encode('utf-8') # 创建DES对象并进行解密 des = DES.new(key, DES.MODE_ECB) decrypted_text = des.decrypt(bytes.fromhex(encrypted_text)).decode('utf-8') # 返回解密结果 return decrypted_text # 测试代码 if __name__ == '__main__': key = '12345678' # 密钥,必须为8字节长度 text = 'Hello, DES!' # 待加密的文本 encrypted_text = des_encrypt(key, text) # 加密 decrypted_text = des_decrypt(key, encrypted_text) # 解密 print('原文:', text) print('密文:', encrypted_text) print('解密后的文本:', decrypted_text) ``` 运行上述代码,可以得到以下输出结果: ``` 原文: Hello, DES! 密文: 1321f7a0e4c5f8ee 解密后的文本: Hello, DES! ``` 可以看到,DES加解密算法已经成功实现。需要注意的是,密钥必须为8字节长度,超过或不足8字节都会导致加解密失败。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timerring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值