Reputation: 83
I am getting this error:
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'
What I am trying to do is create a class with a function (isPrime()) to check primes. Then in a seperate function I check if it is prime (generate_keys()). The class is being called in a seperate file.
Code: Main File where I call the class:
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)
I run this function in this
app.addButton('Encode', encode, 3, 3)
The class code is here:
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
Upvotes: 1
Views: 1068
Reputation: 1123830
You are calling your functions before you have set self.n
:
def __init__(self):
self.p, self.q = self.findingPandQ()
self.n = self.p * self.q
self.n
is set only after you called findingPandQ()
. Because you call isPrime()
from findingPandQ()
, and isPrime()
assumes that self.n
exists, that fails.
You probably made a simple error; you should be using n
(the argument passed to isPrime()
, and not self.n
:
def isPrime(self, n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
Upvotes: 2