【问题标题】:i am getting the following exception while encrypting the file via RSA key instance’s publickey method通过 RSA 密钥实例的 publickey 方法加密文件时出现以下异常
【发布时间】:2016-06-29 08:16:53
【问题描述】:
This is my code for encrypting a file:

我收到以下错误“cipher_aes = AES.new(session_key, AES.MODE_EAX) AttributeError:“模块”对象没有属性“MODE_EAX”” 如果我从第 10 行删除“AES.MODE_EAX”,那么我在第 12 行收到以下错误(密文,标签 = cipher_aes.encrypt_and_digest(data) AttributeError:AESCipher 实例没有属性“encrypt_and_digest”)

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
with open('encrypted_data.bin', 'wb') as out_file:
 recipient_key = open('public.pem').read()
 recipient_key=RSA.importKey(recipient_key)
 session_key = get_random_bytes(16)
 cipher_rsa = PKCS1_OAEP.new(recipient_key)
 out_file.write(cipher_rsa.encrypt(session_key))
 cipher_aes = AES.new(session_key, AES.MODE_EAX)
 data = b'blah blah blah Python blah blah'
 ciphertext, tag = cipher_aes.encrypt_and_digest(data)
 out_file.write(cipher_aes.nonce)
 out_file.write(tag)
 out_file.write(ciphertext)

【问题讨论】:

    标签: python cryptography public-key-encryption


    【解决方案1】:

    我在 the documentation 的任何地方都看不到密码模式 EAX。这表明 PyCrypto 在当前稳定版本 (2.6.1) 中不支持它。

    但是浏览the source code 我看到MODE_EAX 存在。因此您可以尝试使用the latest experimental build,2.7.1,其中支持MODE_EAX。

    但是,根据您对encrypt_and_digest 的使用,我猜您使用的代码不是基于 pycrypto,而是基于 PyCryptodome。我个人不熟悉该库,但它具有您尝试使用的功能。你可以download it here。

    【讨论】:

    • 留下那个参数我亲爱的兄弟你有什么解决办法一个来自第 12 行的错误
    • 请查看编辑,我添加了一个库链接,您需要使用当前在第 12 行引发错误的方法。
    • 这里显示 SORRY / \ / \ 该页面尚不存在。
    • 抱歉链接格式不正确。现在再试一次。
    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多