Kryptografia/miniprojekt1/elgammal.py
2021-10-28 16:26:19 +02:00

46 lines
1.9 KiB
Python

# elgammal
import copy
c1 = 4930490882687076116085929823448518556524926336621082524571007069499319395855324296502235437445967934932787521600599107532531078194796843391952627184815516623257150401617528223084265470741645331148385802788497019587076927091538268120958170365588158668012464348961057111168929144800733802189147469833339
c2 = 1320778616639746095525730330254407469921408577233897503987468020505370909557234769852635541377723891236681125391568276272644732373241015100686633081535309319866887351174142276504736818002775011538657454525452322932342497332733737438842353481743010298952515756561095990344099481063406400709632171512866
x = 1831848944654438578680931843323781967581951359544432469465859793716377126588722692849486241121314279763178719116053330559415007978747231329088425724769619907558118568416619870417918752065421786996731037566984329764120000127110604671799692639001148712141410776812882409354267203252047876023101514203171
p = 6262263371377541559141206895621884814807915303145516042650825916222136098970293814810224532254572606923485012865464653542596037422501455912488470111758490086095137370045391054577237580171284636282615725496568360590261691887179088064684821943355507214597667739684622993519703639685862165456606534344947
def euklides(x, N):
A,B,U,V = N, x, 0, 1
while True:
q = A // B
A_new = copy.deepcopy(B)
B = A - B * q
A = A_new
U_new = copy.deepcopy(V)
V = U - V * q
U = U_new
if B == 0:
break
d, u = A, U
v = (d - x * u)/N
return u, v, d
def odwrotnosc(b, n):
u, _, d = euklides(b, n)
if d == 1:
return u % n
else:
return False
def potegowanieimod(b, k, n):
y = 1
while k > 0:
if k & 1:
y = y*b % n
b = b**2 % n
k = k >> 1
return y
m = (c2 * potegowanieimod(odwrotnosc(c1, p), x, p)) % p
print(m)