簡體   English   中英

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

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

我需要使用 python 加密一個大文件。 做一些在線研究向我推薦了幾個模塊。 我有點困惑選擇使用什么。

我想確保它必須盡可能高效地與 python 一起工作。

什么是最高效的 python AES 256 加密模塊?

如果您的硬件支持,您可以嘗試支持 AES-NI 模式的“ PyCryptodome ”包。 PyCryptodome 支持使用 256 位密鑰的 AES 加密。 如果您尋求加密+身份驗證,則可以使用 GCM 模式。 如果身份驗證不符合您的興趣,您可以使用 CTR 模式。

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)

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")

要檢查您的硬件是否支持 AES-NI 模式,您可以在此處查看答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM