2

I am writing a program to encrypt a file using RSA algo in python without using Crypto library. i have generated the keys and the e, n and d are stored in a .pem file. Now in another strict where the encrypting is taking place i am using the e, d and n values, but every time i am running the script an error is showing :

 File "rsaencrypt.py", line 91, in <module>
 main()
 File "rsaencrypt.py", line 62, in main
 encrypt = pow(content, e, n)      
 TypeError: unsupported operand type(s) for pow(): 'bytes','_io.TextIOWrapper', '_io.TextIOWrapper'

heres how i am opening the file in the encryption script and using pow() to encrypt the files:

    n = open('nfile.pem', 'r')
    c = open('cfile.pem', 'r')
    d = open('dfile.pem', 'r'))
    encrypt = pow(content, e, n) 

I have searched the internet for how to read the int value from a file but i have found nothing.

Heres how i am saving the values in efile, dfile, and nfile:

#saving the values of n, d and e for further use    
efile = open('efile.pem', 'w')
efile.write('%d' %(int(e)))
efile.close()

dfile = open('dfile.pem', 'w')
dfile.write('%d' %(int(d)))
dfile.close()

nfile = open('nfile.pem', 'w')
nfile.write('%d' % (int(n)))
nfile.close()

the values are stored like this: 564651648965132684135419864..............454

Now as want to encrypt the files i need to read the integer values written in the efile, dfile and nfile to use the values in the pow() as arguments.

Looking forward to suggestions. Thank you.

Sandipan
  • 683
  • 8
  • 25
  • have you tried casting the read value to an int? Like you do when writing to the file `int(value)` – jmoney Dec 03 '18 at 05:30
  • yes i did like this n=int(open('filename', 'r')) , but then another error is showing int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper' – Sandipan Dec 03 '18 at 05:33

2 Answers2

3

The open() function returns a file object, not the int. You need to convert returned object into int value by:

n = open('nfile.pem', 'r')
n_value = int(list(n)[0])

etc.

Another option (same result) is:

n = open('nfile.pem', 'r')
n_value = int(n.read())
Denis Rasulev
  • 3,744
  • 4
  • 33
  • 47
  • its showing an error in the pow() like : TypeError: unsupported operand type(s) for pow(): 'bytes', 'int', 'int' – Sandipan Dec 03 '18 at 05:42
  • as i am using the values as args to pow() i need the contents in integer form that are written in the file – Sandipan Dec 03 '18 at 05:47
  • 1
    That's correct :) This answer may be useful: https://stackoverflow.com/questions/444591/convert-a-string-of-bytes-into-an-int-python – Denis Rasulev Dec 03 '18 at 05:48
  • oh thank you, how can i miss it. The error is in the ''contents" not in the e or n – Sandipan Dec 03 '18 at 05:57
1

The recommended way is to use with, this ensures your file is closed once you are done with it rather than waiting for garbage collection or explicitly calling f.close() to close your file.

n_results = []

with open('nfile.pem', 'r') as f:
    for line in f:
        #do something
        try:
            n.append(int(i))
        except TypeError:
            n.append(0) #you can replace 0 with any value to indicate a processing error

Also, utilize try-except block in case you have noise in your file that cannot be converted to integers. n_results return a list of all your values from your files which you can use to aggregate or combine them later for a single output.

This would be a better foundation as your project scales and if you deal with more data.

BernardL
  • 5,162
  • 7
  • 28
  • 47