def decode(self, text, key): self.decoded_text = "" self.secret_key_mod_inverse = cu.modular_inverse( key, self.alphabet_size) for symbol in text: index_in_alphabet = self.legal_alphabet.index(symbol) self.decoded_text += self.legal_alphabet[ (index_in_alphabet * self.secret_key_mod_inverse) % self.alphabet_size] return self.decoded_text
def decode(self, text, key): self.decoded_str = "" #modular_inverse returnerer modulo-inversen, a = nøkkel, m= lengden av alfabetet #OBS: En nøkkel n virker bare bare dersom den har en modulo-invers for character in text: index = self.legal_alphabet.index(character) new_key = cu.modular_inverse(key, self.alphabet_size) self.decoded_str += self.legal_alphabet[(index * new_key) % self.alphabet_size] return self.decoded_str
def generate_keys(self): p = cu.generate_random_prime(8) q = p _gcd = 0 while q == p or _gcd != 1: q = cu.generate_random_prime(8) p = cu.generate_random_prime(8) phi = (p - 1) * (q - 1) e = random.randint(3, phi - 1) _gcd = gcd(e, phi) print("p: ", p, "q: ", q, "gcd: ", _gcd) n = p * q d = cu.modular_inverse(e, phi) self.sender_key = (n, e) self.key = (n, d)
def generate_keys(self, key): return cu.modular_inverse(key, self.alphabet_size)