def primitiveRoot(n): # if isPrime(n) == False: # return -1 phi = n - 1 s = prime5Divisors(phi) # Check for every number from 2 to phi for i in range(2, phi + 1): flag = False for it in s: if power(i, phi // it, n) == 1: flag = True break if flag == False: return i return -1
from isPrime import is_prime print(is_prime(13)) from extendEuclid import egcd print(egcd(40, 100)) from modularPower import power print(power(2, 5, 13)) from base26Cipher import enCrypt, deCrypt print(deCrypt('HoangThiLinh')) print(enCrypt(294))
def __init__(self, p, a, k): self.p = p self.a = a self.k = k self.alpha = Prime.primitiveRoot(p) self.beta = power(self.alpha, a, p)
def deCode(self, y1, y2): dy1 = power(y1, self.p - self.a - 1, self.p) x = ((y2 % self.p) * dy1) % self.p return (x, dy1)
def enCode(self, x): y1 = power(self.alpha, self.k, self.p) y2 = ((x % self.p) * power(self.beta, self.k, self.p)) % self.p return (y1, y2)