Home > database >  In python, open.write() gives me an error
In python, open.write() gives me an error

Time:10-04

This Code runs and saves all the values in my output text file till d but does not saves my value of public_key and private_key.it is a simple code in which i am trying to replicate RSA algorithm and trying to save all the values in a text file with open.write(). Can anyone tell me where it is wrong? It looks like this:

import random
import time
import os
max_PrimLength = 1000000000000

output_folder_name=f"output/{int(time.time()*100)}"
os.makedirs(f"{output_folder_name}")
details_file=open(f"{output_folder_name}/details.txt","a ")

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)
    
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

def is_prime(num):
    if num == 2:
        return True
    if num < 2 or num % 2 == 0:
        return False
    for n in range(3, int(num**0.5) 2, 2):
        if num % n == 0:
            return False
    return True

def generateRandomPrim():
    while(1):
        ranPrime = random.randint(0,99999)
        if is_prime(ranPrime):
            return ranPrime

p = generateRandomPrim()
q = generateRandomPrim()
h = p*q
phi = (p-1) * (q-1) 
e = random.randint(1, phi)
g = gcd(e,phi)
while g != 1:
    e = random.randint(1, phi)
    g = gcd(e, phi)
        
d = egcd(e, phi)[1]
d = d % phi
if(d < 0):
    d  = phi

print("First Prime Number(p): %d" % p)
details_file.write("First Prime Number(p): %d\n" % p)
print("Second Prime Number(q): %d" % q)
details_file.write("Second Prime Number(q): %d\n" % q)
print("h: %d" %h)
details_file.write("h: %d\n" %h)
print("phi(p-1)(q-1): %d" % phi)
details_file.write("phi(p-1)(q-1): %d\n" % phi)
print("e: %d" % e)
details_file.write("e: %d\n" %e)
print("d: %d" % d)
details_file.write("d: %d\n" % d)

def generate_keyPairs():
     return ((e,h),(d,h))

if _name_ == 'main':
    public_key,private_key = generate_keyPairs() 
    print("Public_Key(Traditional_RSA): ",public_key)
    details_file.write("Public_Key(Traditional_RSA): \n",public_key)
    print("Private_Key(Traditional_RSA): ",private_key)
    details_file.write("Private_Key(Traditional_RSA): \n",private_key)
    
details_file.close()

Trackback Error occured:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-3018b2703be1> in <module>
    144     public_key,private_key = generate_keyPairs()
    145     print("Public_Key(Traditional_RSA): " ,public_key)
--> 146     details_file.write("Public_Key(Traditional_RSA): \n" ,public_key)
    147     print("Private_Key(Traditional_RSA): " ,private_key)
    148     details_file.write("Private_Key(Traditional_RSA): \n" ,private_key)

TypeError: write() takes exactly one argument (2 given)

CodePudding user response:

The error is you are giving 2 parameters to write, use this:

...
details_file.write(f"Public_Key(Traditional_RSA): \n{public_key}")
...
details_file.write(f"Private_Key(Traditional_RSA): \n{private_key}")

also the __name__ == "__main__" is used to run the code block only when file has been run directly, i.e

> python this_file_name.py

But if you import this file to another file like:

# other_file.py
import this_file_name

and run that file:

python other_file.py

Then code block under if __name__ == "__main__": will not run.

CodePudding user response:

You are giving 2 arguments to the write() function but it only expects one - the string you want to write to the file. You should format the arguments into a single strings for your last two calls to write(). Something like this:

details_file.write("Public_Key(Traditional_RSA):"   str(public_key)   "\n")
details_file.write("Private_Key(Traditional_RSA):"   str(private_key)   "\n")
  • Related