def elGamalEncrypt(A, b, r_s, p_s, message): B = modexp(r_s, b, p_s) encrypted = modexp(message * modexp(A, b, p_s), 1, p_s) print("[B, encrypted]") print([B, encrypted]) return ([B, encrypted])
def elGamalDecrypt(B, a, p_s, encrypted): R = modexp(B, a, p_s) invR = multinv(R, p_s) decrypted = modexp(encrypted * invR, 1, p_s) print("[decrypted]") print([decrypted]) return ([decrypted])
def request(a, m_s, N_s, prime1, prime2, e_s, r_r, p_r): A = modexp(r_r, a, p_r) d = multinv(e_s, (prime1 - 1) * (prime2 - 1)) signature = modexp(m_s, d, N_s) print("[A, Ns, es, ms^ds] =") print([A, N_s, e_s, signature]) return ([A, N_s, e_s, signature])
def probableprime(n, t): random.seed() for _ in range(t): # t tries a = random.randint(2, n - 1) assert a >= 2 assert a < n x = modexp(a, n - 1, n) if 1 != x: return False return True
def primality(N): ''' Input: Positive integer N Output: yes/no ''' # Pick a positive integer a < N at random random.seed(time.time()) a = random.randint(1, N - 1) if modexp.modexp(a, N - 1, N) == 1: return True else: return False
def primality2(N): ''' Input: Positive integer N Output: yes/no ''' # Pick a positive integer a1, a2, ..., ak < N at random k = 8 for i in range(8): random.seed(time.time()) a = random.randint(1, N - 1) if modexp.modexp(a, N - 1, N) == 1: continue else: return False return True
def testNumber(a,n): if isprime(n): return True res = modexp(a, n-1, n) return res != 1
def checkDigitalSignature(A, N, e, S, m_s): signature = modexp(S, e, N) if signature != m_s: print("Signature check fails.") else: print("Signature check success.")