【问题标题】:Encrypt file in python with aes使用 aes 在 python 中加密文件
【发布时间】:2015-08-14 07:30:27
【问题描述】:

我想在 python 中以 cbc 模式使用 aes 128 加密和解密文件(任何类型的文件)。

我对密码学很陌生,我尝试了一些教程,但都只适用于文本,我需要它来处理文件。

谁能给我一个解决方案?

【问题讨论】:

标签: python encryption aes


【解决方案1】:

通过 Google 快速搜索,我找到了 Crypto 包。它带有我正在使用的 iPython,但无论如何安装应该很简单。

我只是repost这里的示例供您参考。

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'

Here 是 AES 的文档。

如果您尝试加密文件,您可以使用 openSSL 或使用 Thijs 提供的 Crypto 的 Python 解决方案。点击here了解更多信息。

【讨论】:

  • 请注意,该库已被废弃多年。
猜你喜欢
  • 2018-08-21
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多