在 python 中,open.write() 给我一个错误

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

此代码运行并保存我的输出文本文件中的所有值,直到 d 但不保存我的 public_key 值,private_key.it 是一个简单的代码,我在其中尝试复制 RSA 算法并尝试使用 open.write() 将所有值保存在文本文件中。谁能告诉我哪里错了? 它看起来像这样:

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()

发生引用错误:

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)

错误是您给 write 提供了 2 个参数,使用这个:

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

另外 __name__ == "__main__" 仅在文件直接 运行 时用于 运行 代码块,即

> python this_file_name.py

但是如果您将此文件导入另一个文件,例如:

# other_file.py
import this_file_name

和运行那个文件:

python other_file.py

那么if __name__ == "__main__":下的代码块就不会运行.

您为 write() 函数提供了 2 个参数,但它只需要一个 - 您要写入文件的字符串。您应该将最后两次调用 write() 的参数格式化为单个字符串。像这样:

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