This commit is contained in:
Jakub Adamski 2021-11-04 15:03:24 +01:00
parent e0c2e94698
commit 695672a7dd
4 changed files with 110 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

51
functions/functions.py Normal file
View File

@ -0,0 +1,51 @@
import random, copy
#sprawdza czy liczby są pierwsze
def fermat(dokladnosc, liczba):
i = 0
while i < dokladnosc:
a = random.randint(1, liczba - 1)
if pow(a, (liczba - 1), liczba) == 1:
i = i + 1
else:
return False
return True
#x * u + N * v = d
#Jeśli d == 1 to liczby są względnie pierwsze
#d to NWD
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
#liczba odwrotna do b w modulo n
def odwrotnosc(b, n):
u, _, d = euklides(b, n)
if d == 1:
return u % n
else:
return False
#potegowanie x do k w mod n
def potegowanieimod(x, k, n):
y = 1
while k > 0:
if k & 1:
y = y*x % n
x = x**2 % n
k = k >> 1
return y

44
zajecia3/rsa.py Normal file
View File

@ -0,0 +1,44 @@
# rsa
import sys, random
sys.path.append("../")
import functions.functions as fn
#generowanie kluczy
pierwsze = []
while True:
a = random.getrandbits(512)
if (fn.fermat(100, a)):
pierwsze.append(a)
if len(pierwsze) == 2:
break
n = pierwsze[0] * pierwsze[1]
fi = (pierwsze[0] - 1) * (pierwsze[1] - 1)
e = None
while True:
a = random.getrandbits(fi.bit_length() - 1)
if fn.fermat(100, a):
_, _, d = fn.euklides(a, fi)
if d == 1:
e = a
break
d = fn.odwrotnosc(e, fi)
print("\nKlucz publiczny: ({},\n {})".format(n, e))
print("\nKlucz tajny: ({}, {})".format(n, d))
#szyfrowanie
m = random.randint(1, 512)
print("\nWiadomośc: {}".format(m))
c = fn.potegowanieimod(m, e, n)
print("\nSzyfrogram: {}".format(c))
#odszyfrowanie
M = fn.potegowanieimod(c, d, n)
print("\nWiadomość odszyfrowana: {}\n".format(M))

14
zajecia3/testowe.py Normal file
View File

@ -0,0 +1,14 @@
import sys
sys.path.append("../")
import functions.functions as fn
c = 1194024449148901642095200097870055737741645764394744155318695645194596220391443343197027572364026243089002152070134768229865572977202072275964098840721025876961903699837107178456937960769036226808529625975259747392533732677170542802757258980865413340360159618466950580761168296729593382939093152458632579807462168306693685058458109946909424522629476866018486300768172119332072738020220096063821408769836161596668177421022164759936875707676886053295432123403900793891136494095488406192842296217375007110677424647572379883138029436045378765616434346276790888927916404956009509484434114877972302768109847
d = 44172166664316348618184176869841529130125229257274245655212364632114548140974575751803935492101214210349057569007558651942740773243587655474183506994622943062602636333215858034182100779227210047231793514705323804472916623002482317841498373311321562419819278686569667913165742033122259784921734617682
n = 3490883977143252259982384869391866343672985433713547441739689121492674315619580156830620326021421471792926011913651839538684958739735100518286394257159090517265115820506822274157397650474074752645311314258176271561685463992191717804891559250609466279443046524956709382997523640335221229619946221067286482010901829387658904241388693494999300615059965292460448560270456473127070077286237984616794336775697861678085567236666894286408386466385344576002768829158953193122127573651343254040156954992093901684632064068184042356933091345978630925116597494256095547989334592457862346351113558398240600445500393
M = fn.potegowanieimod(c, d, n)
print("\nWiadomość odszyfrowana: {}\n".format(M))