0

I have been using RSA Public/Private Key Pair to encrpyt data:

random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
publickey = key.publickey()

and write it to a file. The probelm I am getting is when I read any file in bytes:

f = open('test','rb')
d = f.read()
enc_data = publickey.encrypt(d,32)

I am getting the encrypted data as a tuple

>>> type(enc_data)
>>> <class 'tuple'>

The problem is when I try to write the encrypted text in any newly created file I am not able to do it in any mode

o = open('out','wb') #same with 'w' mode
o.write(enc_data)

It displays the error:

Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
o.write(enc_data)
TypeError: must be str, not tuple

How to get my encrypted data saved?

Manuj Mittal
  • 97
  • 1
  • 2
  • 12

1 Answers1

0

According to these docs, the encrypt function returns a tuple, where the second value is always None. I'm assuming this is to support backwards compatibility. Try:

enc_data, other =  publickey.encrypt(d,32)

Then write enc_data (this is called "ciphertext")

In the future, it would be a good idea to explicitly say what library you are using. Also emcrytp...

user1302130
  • 456
  • 5
  • 9
  • that asked for 1 more value, but I got my answer from the duplicate suggestion by Garrett Hyde! :) Thanks a lot btw :) – Manuj Mittal Jan 13 '16 at 19:26