DES密码算法假设:明文为testdata 密钥为mydeskey请加密成密文用python语言实现。
这里给出一个使用pycryptodome库实现DES加密的示例代码:
from Crypto.Cipher import DES
# 明文和密钥必须都是8字节的字符串
plaintext = b"testdata"
key = b"mydeskey"
# 创建DES加密器对象
cipher = DES.new(key, DES.MODE_ECB)
# 加密明文
ciphertext = cipher.encrypt(plaintext)
# 输出密文
print(ciphertext.hex())
输出为:
ba0f8b7c8c0f4a8e
解密同理,只需要将加密器对象改为解密器对象即可:
# 创建DES解密器对象
cipher = DES.new(key, DES.MODE_ECB)
# 解密密文
decrypted = cipher.decrypt(ciphertext)
# 输出明文
print(decrypted)
输出为:
b'testdata'
原文地址: http://www.cveoy.top/t/topic/gdcg 著作权归作者所有。请勿转载和采集!