Error when trying to encrypt data python/rsa framework?

5e0dddfbdf335170623275.png
The error itself:
Traceback (most recent call last):
 File "C:\Users\root\Documents\Python Projects\Tests\main.py", line 30, in BtnStartEncrypt
self.ReturnEncryptData()
 File "C:\Users\root\Documents\Python Projects\Tests\main.py", line 38, in ReturnEncryptData
 encrypt_data = rsa.encrypt(data, public_key)
 File "C:\Users\root\AppData\Local\Programs\Python\Python38\lib\site-packages\rsa\pkcs1.py", line 171, in encrypt
 keylength = common.byte_size(pub_key.n)
AttributeError: 'str' object has no attribute 'n'


A function that is not working:
def ReturnEncryptData(self):
 public_key = self.ui.TextPublicKeyEncrypt.text() # QLineText
 data = self.ui.TextEncrypt.toPlainText().encode('utf8') # QEditText
 if data and public_key:
 encrypt_data = rsa.encrypt(data, public_key) # pass the data entered by the user in QEditText and QLineEdit
 return self.ui.TextEncrypt.setText(encrypt_data) # return encrypted data back to QEditText


I believe that the error may be that the rsa library does not understand that I gave the public key, struggling with that issue for the second day as I can not decide on StackOverFlow and in Google found no answer :(
April 4th 20 at 00:33
1 answer
April 4th 20 at 00:35
You are passing it is not a key, and a string. How to convert a string in a key, read the docks for the library.
So I thought, until I found only this function:


encrypt_data = rsa.encrypt(data, rsa.PublicKey(public_key))


But after her call get:


TypeError: __init__() missing 1 required positional argument: 'e'


I do not understand what the parameter 'e' in the General rummage on) - Marion49 commented on April 4th 20 at 00:38
@Marion49, you dock read? - Leslie commented on April 4th 20 at 00:41
@Marion49, here's what you do https://stuvel.eu/python-rsa-doc/reference.html#rs... there is clearly stated that you need two integers n and e (what is that number learn read basics about RSA) Just below, are three ways to download these keys load_* Your line should be put in any of these formats, convert to bytes and load the appropriate method. - Leslie commented on April 4th 20 at 00:44
@Leslie,

Yes I read and tried this:


def ReturnEncryptData(self):
 public_key = bytes(self.ui.TextPublicKeyEncrypt.text(), 'utf-8')
 data = self.ui.TextEncrypt.toPlainText().encode('utf8')
 rsa.PublicKey.save_pkcs1(public_key, format='PEM')
 if data and public_key:
 encrypt_data = rsa.encrypt(data, rsa.PublicKey.load_pkcs1(public_key, format='PEM'), 'utf-8')


Displays this error:


AttributeError: 'bytes' object has no attribute '_save_pkcs1_pem'


Studying the library more)
- Marion49 commented on April 4th 20 at 00:47
@Marion49, where I wrote about save_pkcs? - Leslie commented on April 4th 20 at 00:50
@Leslie, I tried without them, same error was :(

Here's precisely:


AttributeError: 'bytes' object has no attribute 'n'


So I tried:


public_key = bytes(self.ui.TextPublicKeyEncrypt.text(), 'utf-8')
 data = self.ui.TextEncrypt.toPlainText().encode('utf8')
self.ui.statusBar.clearMessage()
 if data and public_key:
 encrypt_data = rsa.encrypt(data, public_key)
- Marion49 commented on April 4th 20 at 00:53

Find more questions by tags PythonRSA