RSA decryption of AES Session key fails with 'AttributeError: 'bytes' object has no attribute 'n'

[亡魂溺海] 提交于 2020-12-01 11:07:37

问题


I'm working on implementing a public key encryption from PyCryptodome on Python 3.6. When I try to create a symmetric encryption key and encrypt/decrypt variables, it all works fine. But the minute I introduce RSA (and PKCS1_OAEP), it all goes down the tubes - the session_key encrypts fine but when I try and decrypt it, I get the following error:

Traceback (most recent call last):
  File "enctest.py", line 109, in <module>
    deckey = decrypt_val(enckey)
  File "enctest.py", line 77, in decrypt_val
    session_key = cipher.decrypt(ciphertext)
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 187, in decrypt
    modBits = Crypto.Util.number.size(self._key.n)
AttributeError: 'bytes' object has no attribute 'n'

My code is as follows. Can anyone take a look and tell me what I'm doing wrong?

from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Random import get_random_bytes

random_generator = Random.new().read
keys = RSA.generate(1024, random_generator)
pubkey = keys.publickey()
privkey = keys.exportKey()
pubcipher = PKCS1_OAEP.new(pubkey) # ciphertext = cipher.encrypt(message)
privcipher = PKCS1_OAEP.new(privkey)  # message = cipher.decrypt(ciphertext)
privkeystr = keys.exportKey(format='PEM', passphrase=None, pkcs=1)
pubkeystr = keys.publickey().exportKey(format='PEM', passphrase=None, pkcs=1)

def encrypt_val(session_key, cipher = pubcipher):
    try:
        session_key = session_key.encode('utf8')
    except:
        pass
    ciphertext = cipher.encrypt(session_key)
    print("encrypted key : %s \n" % ciphertext)
    return ciphertext


def decrypt_val(ciphertext, cipher = privcipher):
    session_key = cipher.decrypt(ciphertext)
    try:
        session_key = session_key.decode('utf8')
    except:
        pass
    return session_key

def aesenc(data):
    try:
        data = data.encode('utf8')
    except:
        pass
    key = get_random_bytes(16)
    cipher = AES.new(key, AES.MODE_EAX)
    ciphertext, tag = cipher.encrypt_and_digest(data)
    aesencdict = {'aesdict' : {'ciphertext' : ciphertext, 'tag' : tag, 'nonce' : cipher.nonce} , 'key' : key}
    return(aesencdict)

def aesdec(aesdict, key):
    cipher = AES.new(key, AES.MODE_EAX, aesdict['nonce'])
    data = cipher.decrypt_and_verify(aesdict['ciphertext'], aesdict['tag'])
    try:
        data = data.decode('utf8')
    except:
        pass
    return data


val = "hello"
encval = aesenc(val)
enckey = encrypt_val(encval['key'])
print(enckey)
deckey = decrypt_val(enckey)
print(deckey)
if deckey == encval['key']:
    outval = aesdec(encval['aesdict'], encval['key'])
    print(val, outval)
else:
    print("oops\n")

回答1:


It seems you do a spurious export, which translates a key into the encoding of a key:

privkey = keys.exportKey()
....
privcipher = PKCS1_OAEP.new(privkey)  # message = cipher.decrypt(ciphertext)

after which it tries to find the modulus n from the encoded key instead of from the object instance that contains a member n.

Try:

privcipher = PKCS1_OAEP.new(keys)

instead.



来源:https://stackoverflow.com/questions/44790857/rsa-decryption-of-aes-session-key-fails-with-attributeerror-bytes-object-has

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!