简体   繁体   English

在python中进行AES 256位加密的有效方法

[英]Efficient method to do AES 256bit encryption in python

I need to encrypt a large file using python.我需要使用 python 加密一个大文件。 Doing some online research suggested me several modules.做一些在线研究向我推荐了几个模块。 I am a bit confused to chose what to use.我有点困惑选择使用什么。

I want to make sure that it must work efficiently as much as possible with python.我想确保它必须尽可能高效地与 python 一起工作。

What is the most efficient python AES 256 encryption module?什么是最高效的 python AES 256 加密模块?

You can try ' PyCryptodome ' package that supports AES-NI mode if your hardware supports it.如果您的硬件支持,您可以尝试支持 AES-NI 模式的“ PyCryptodome ”包。 PyCryptodome supports AES encrypion with 256 bits key. PyCryptodome 支持使用 256 位密钥的 AES 加密。 You can use GCM mode if you seek for encryption+authentication.如果您寻求加密+身份验证,则可以使用 GCM 模式。 If authentication is not in your interest you can use CTR mode.如果身份验证不符合您的兴趣,您可以使用 CTR 模式。

Code example for CTR mode: CTR 模式的代码示例:

from Crypto.Cipher import AES
# encryption
    cipher = AES.new(key_bytes, AES.MODE_CTR, use_aesni='True')
    ciphertext = cipher.encrypt(data_as_bytes)
    nonce = cipher.nonce
# decryption
    cipher = AES.new(key_bytes, AES.MODE_CTR, nonce=nonce, use_aesni='True')
    plaintext_as_bytes = cipher.decrypt(ciphertext)

Code example for GCM mode: GCM 模式的代码示例:

from Crypto.Cipher import AES
# encryption
    cipher = AES.new(key_bytes, AES.MODE_GCM, use_aesni='True')
    nonce = cipher.nonce
    ciphertext, tag = cipher.encrypt_and_digest(data_as_bytes)
# decryption
    cipher = AES.new(key_bytes, AES.MODE_GCM, nonce=nonce, use_aesni='True')
    plaintext_as_bytes = cipher.decrypt(ciphertext)
    try:
         cipher.verify(tag)
         print("The message is authenticated")
    except ValueError:
         print("Key incorrect or message corrupted")

To check if your hardware supports AES-NI mode you can check the answers here .要检查您的硬件是否支持 AES-NI 模式,您可以在此处查看答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM