酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Python 代码实现 DES 算法加解密,附示例及安全提示

以下是使用 Python 实现 DES 算法的示例代码: ```python from Crypto.Cipher import DES from Crypto.Util.Padding import pad, unpad def des_encrypt(key, plaintext): cipher = DES.new(key, DES.MODE_ECB) padded_plaintext = pad(plaintext.encode(), DES.block_size) ciphertext = cipher.encrypt(padded_plaintext) return ciphertext def des_decrypt(key, ciphertext): cipher = DES.new(key, DES.MODE_ECB) plaintext = cipher.decrypt(ciphertext) unpadded_plaintext = unpad(plaintext, DES.block_size) return unpadded_plaintext.decode() # 示例用法 key = b'12345678' # 8 字节的密钥 plaintext = "Hello, DES!" ciphertext = des_encrypt(key, plaintext) print("加密后的密文:", ciphertext) decrypted_text = des_decrypt(key, ciphertext) print("解密后的明文:", decrypted_text) ``` 在实际应用中,需要更严格的密钥管理和处理方式。上述代码仅用于演示目的。 由于 DES 算法在安全性上相对较弱,现代应用中更倾向于使用更强大的加密算法,如 AES 等。 ######[AI写代码神器 | 296点数解答 | 2024-09-24 18:28:29]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]