gpt4 book ai didi

python - 属性错误: 'Rsa' object has no attribute 'n'

转载 作者:行者123 更新时间:2023-12-01 02:40:37 29 4
gpt4 key购买 nike

我收到此错误:

Traceback (most recent call last):
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaMainFrame.py", line 7, in <module>
rsa = RsaEncryptionAndDecryption.Rsa()
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 8, in __init__
self.p, self.q = self.findingPandQ()
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 80, in findingPandQ
while not self.isPrime(self.a):
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 14, in isPrime
for i in range(2,int(self.n**0.5)+1):
AttributeError: 'Rsa' object has no attribute 'n'

我想做的是创建一个带有函数(isPrime())的类来检查素数。然后在一个单独的函数中,我检查它是否是素数(generate_keys())。该类在单独的文件中被调用。

代码:我调用该类的主文件:

from Rsa import RsaEncryptionAndDecryption
from appJar import gui

app = gui()
rsa = RsaEncryptionAndDecryption.Rsa()

def encode(name):
msg = app.getEntry('Input to Encode Here')
if msg != '':
p, q = rsa.findingPandQ()

while p == q:
p, q = rsa.findingPandQ()

n, e, d = rsa.generate_keys(p, q)

print(n, e, d)

我在这个中运行这个函数

   app.addButton('Encode', encode, 3, 3)

类代码在这里:

import random
from math import gcd

class Rsa:

def __init__(self):
self.p, self.q = self.findingPandQ()
self.n = self.p * self.q
self.phi = (self.p - 1) * (self.q - 1)
self.e = 0

def isPrime(self, n):
for i in range(2,int(self.n**0.5)+1):
if self.n%i == 0:
return False

return True

def modReverse(self, phi, e):
self.pos00 = self.phi
self.pos01 = self.phi
self.pos10 = self.e
self.pos11 = 1
self.newpos10 = 0


while self.newpos10 != 1:
self.pos00pos10int = self.pos00 // self.pos10
self.inttimespos10 = self.pos00pos10int * self.pos10
self.inttimespos11 = self.pos00pos10int * self.pos11

self.newpos10 = self.pos00 - self.inttimespos10
if self.newpos10 < 0:
self.newpos10 %= phi
self.newpos11 = self.pos01 - self.inttimespos11
if self.newpos11 < 0:
self.newpos11 %= phi

self.pos00 = self.pos10
self.pos01 = self.pos11
self.pos10 = self.newpos10
self.pos11 = self.newpos11


return self.newpos11

def coprime(self, a, b):
return gcd(self.a, self.b) == 1

def findingPandQ(self):
self.a = random.randint(10,100)
while not self.isPrime(self.a):
self.a = random.randint(10,100)

self.b = random.randint(10,100)
while not self.isPrime(self.b):
self.b = random.randint(10,100)
return self.a, self.b

def generate_keys(self, p, q):


for i in range(self.phi):
if self.isPrime(i):
if self.coprime(i, self.phi):
self.e = i

self.d = self.modReverse(self.phi, self.e)

while self.e == self.d:
self.p, self.q = self.findingPandQ(self.p, self.q)

while self.p == self.q:
self.p, self.q = self.findingPandQ(self.p, self.q)
self.n, self.e, self.d = self.generate_keys(self.p, self.q)
return self.n, self.e, self.d

最佳答案

您在设置self.n之前调用您的函数:

def __init__(self):
self.p, self.q = self.findingPandQ()
self.n = self.p * self.q

self.n 仅在调用 findingPandQ() 后设置。因为您从 findingPandQ() 调用 isPrime(),并且 isPrime() 假定 self.n 存在,失败了。

您可能犯了一个简单的错误;你应该使用n(传递给isPrime()的参数,而不是self.n:

def isPrime(self, n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

关于python - 属性错误: 'Rsa' object has no attribute 'n' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762517/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com