Noob14
Noob14

Reputation: 175

Convert an element in tuple to a bytes-like object

I'm trying to base64 encode some RSA encrypted data, but the RSA encryption returns a tuple and the base64 encoding requires a bytes-like object.

File "C:\PATH\AppData\Local\Continuum\anaconda3\lib\base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False)

TypeError: a bytes-like object is required, not 'tuple'

I'm looking for suggestions to fix this the best way.

from Crypto.Cipher import AES
from Crypto.PublicKey import RSA

def rsa_encrypt(data):
    return pub_keyObj.encrypt(data, 32)

def rsa_encrypt_base64(data):
    return base64.standard_b64encode(rsa_encrypt(data))


encrypted_data = aes_encode(data, key, iv) #AES encoding is working fine
print("EncryptedString: ", rsa_encrypt_base64(encrypted_data))

Upvotes: 5

Views: 1979

Answers (1)

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11083

In this line return base64.standard_b64encode(rsa_encrypt(data)), add the index of 0 like this:

return base64.standard_b64encode(rsa_encrypt(data)[0])

It will fix your problem.

The problem is rsa_encrypt will returns a tuple with two items. The first item is the ciphertext of the same type as the plaintext (string or long). The second item is always None.

see Here for more information.

Upvotes: 2

Related Questions