AttributeError:'Rsa'对象没有属性'n'

时间:2017-08-18 17:59:54

标签: python python-3.x

我收到此错误:

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

1 个答案:

答案 0 :(得分:2)

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

def __init__(self):
    self.p, self.q = self.findingPandQ()
    self.n = self.p * self.q
只有在您调用self.n后才会设置

findingPandQ()。由于您从isPrime()致电findingPandQ(),而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