5

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))
Community
  • 1
  • 1
Noob14
  • 165
  • 3
  • 12
  • what is the content of that tuple?, can you post an example of data, key and iv? – Netwave Apr 17 '19 at 08:04
  • The content of the tuple is the rsa encryption and something "empty" like (b'.....e\x89, ' ,) where .... represent a lot at bytes. – Noob14 Apr 17 '19 at 08:07
  • data could be a string or something else I want encrypted. key and IV is generated to make the AES encryption.. and it works fine that part. – Noob14 Apr 17 '19 at 08:08

1 Answers1

2

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.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59