【问题标题】:Python AES encryption without extra module没有额外模块的 Python AES 加密
【发布时间】:2025-12-25 20:05:06
【问题描述】:

是否可以在不安装额外模块的情况下使用 AES 加密/解密数据?我需要从C# 发送/接收数据,该数据使用System.Security.Cryptography 参考加密。

更新 我曾尝试使用 PyAES,但它太旧了。我更新了一些东西来使它工作,但它没有。 我也无法安装,因为它的最新版本是3.3,而我的版本是3.4

【问题讨论】:

  • 当然,如果您不介意 AES 速度慢的话,手动执行 AES 并不难。或者,您可以通过 ctypes 访问 Windows 加密 API。或openssl。但是使用一个让它变得简单的模块会好得多。你有不想的理由吗? (注意official documentation 专门指向pycrypto 做这种事情。)
  • C# 部分已经在工作,问题是 Python。我总是在安装 Python 模块时遇到问题,所以如果有可能在没有额外模块的情况下使用 AES,我会很高兴。加密/解密速度最高可达 200 毫秒,用于聊天。
  • 如果您在安装 Python 模块时遇到问题,这是您应该解决的问题,而不是忽略它。如果您的 Python 安装损坏,请修复或重新安装。如果您不了解pipChristoph Gohlke's Windows binary package repo,请了解它们。
  • 另一种可能性:你能用 IronPython 代替 CPython 吗?然后,您可以像从 C# 一样轻松地从 Python 访问 System.Security.Cryptography
  • 自 2015/12/18 起,pyaes 似乎在 python 3.4 上运行良好,至少对于简单的用例(CTR 加密/解密)

标签: python aes python-3.4


【解决方案1】:

带有加密模块的 Python 3.6

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"my deep dark secret")
print(token)


f.decrypt(token)

【讨论】:

    【解决方案2】:

    我正在使用Cryptography 库。

    Cryptography 是一个积极开发的库,它提供 密码配方和原语。它支持 Python 2.6-2.7, Python 3.3+ 和 PyPy。

    Here is an example 如何使用该库:

    >>> import os
    >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
    >>> from cryptography.hazmat.backends import default_backend
    >>> backend = default_backend()
    >>> key = os.urandom(32)
    >>> iv = os.urandom(16)
    >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
    >>> encryptor = cipher.encryptor()
    >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
    >>> decryptor = cipher.decryptor()
    >>> decryptor.update(ct) + decryptor.finalize()
    'a secret message'
    

    【讨论】:

    • 我完全赞同这个答案。 Cryptography 库得到积极维护;其他答案(PyCrypto、PyAES)中提到的库不是。
    • 这个实现似乎比我的 PC 上的 Crypto.Cipher AES 慢(速度的 2/3)..
    【解决方案3】:

    Here is a self-contained implementation of AES compatible with Python 3.

    示例用法:

    aesmodal = AESModeOfOperation() 
    key = [143,194,34,208,145,203,230,143,177,246,97,206,145,92,255,84]
    iv = [103,35,148,239,76,213,47,118,255,222,123,176,106,134,98,92]
    
    size = aesmodal.aes.keySize["SIZE_128"]
    
    mode,orig_len,ciphertext = aesmodal.encrypt("Hello, world!", aesmodal.modeOfOperation["OFB"], key, size, iv)
    print(ciphertext)
    plaintext = aesmodal.decrypt(ciphertext, orig_len, mode, key, size, iv)
    print(plaintext)
    

    【讨论】:

    • 为什么是奇怪的名字? :0
    • 不会因为你的最后一行print plaintext 而在 Python 3 中搞砸。我认为您需要将其称为函数。
    • 链接不正确。
    【解决方案4】:

    PYAES 应该适用于任何版本的 Python3.x。无需修改库。

    这是 Python3.x 的 pyaes CTR 模式的完整工作示例 (https://github.com/ricmoo/pyaes)

    import pyaes
    
    # A 256 bit (32 byte) key
    key = "This_key_for_demo_purposes_only!"
    plaintext = "Text may be any length you wish, no padding is required"
    
    # key must be bytes, so we convert it
    key = key.encode('utf-8')
    
    aes = pyaes.AESModeOfOperationCTR(key)    
    ciphertext = aes.encrypt(plaintext)
    
    # show the encrypted data
    print (ciphertext)
    
    # DECRYPTION
    # CRT mode decryption requires a new instance be created
    aes = pyaes.AESModeOfOperationCTR(key)
    
    # decrypted data is always binary, need to decode to plaintext
    decrypted = aes.decrypt(ciphertext).decode('utf-8')
    
    # True
    print (decrypted == plaintext)
    

    如果您遇到任何错误,请告诉我

    【讨论】:

      【解决方案5】:

      添加到@enrico.bacis 的答案:标准库中没有实现 AES。它在 PyCrypto 库中实现,该库稳定且经过良好测试。如果您需要 AES,请将 PyCrypto 添加为代码的依赖项。

      虽然 AES 原语在理论上足够简单,您可以用纯 Python 编写它们的实现,但强烈建议您不要这样做。这是加密的第一条规则:不要自己实现。特别是,如果您只是在推出自己的加密库,那么您几乎肯定会让自己面临某种侧信道攻击。

      【讨论】:

      • 我不能使用 PyCrypto,因为它不包括 Python 3.4
      【解决方案6】:

      标准库中可用的加密服务是those。如您所见,AES 未列出,但建议使用pycrypto,这是一个额外模块

      您只需使用 pipeasy_install 安装它,然后如 pycrypto 页面所示:

      from Crypto.Cipher import AES
      obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
      message = "The answer is no"
      print obj.encrypt(message)
      

      不使用额外模块的唯一另一种方法是自己编写函数代码,但是下载额外模块并使用它有什么区别?

      如果您想要一个纯 Python 的 AES 实现,您可以下载并导入检查 pyaes

      【讨论】:

      • 好吧,你自己编写一个额外的模块,用纯 Python 编写,并保留在与主脚本相同的项目目录中,可以解决“安装模块时遇到问题”的方法。不一定是的方式,但肯定不同于安装pycrypto
      • 我在回答中说的是,如果您不这样做,您也可以下载完整的 pycrypto 源并将其放在您的代码旁边不想(或不能)安装模块。
      • 这只适用于纯 Python 包; PyCrypto 主要是你必须构建的 C 扩展。
      • This 只到 Python 3.3 而我有 Python 3.4
      • 那也不行,版本是also to old。或者你认为如果我修复 print() 会起作用?