diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/__pycache__/dsa.cpython-310.pyc b/__pycache__/dsa.cpython-310.pyc new file mode 100644 index 0000000..fa97cc3 Binary files /dev/null and b/__pycache__/dsa.cpython-310.pyc differ diff --git a/dsa.py b/dsa.py index 404adf1..7c8310e 100644 --- a/dsa.py +++ b/dsa.py @@ -176,15 +176,15 @@ def ver_DSA(p, q, g, y, m, r, s): return b -print("DSA") -klucz_publiczny_DSA, klucz_prywatny_DSA = genDSA() -print(klucz_publiczny_DSA, klucz_prywatny_DSA) -p, q, g, y = klucz_publiczny_DSA -x = klucz_prywatny_DSA - -m = "Thats my Kung Fu".encode("ASCII") -print(m) -r, s = sig_DSA(p, q, g, x, m) -print(r, s) -b = ver_DSA(p, q, g, y, m, r, s) -print(b) +# print("DSA") +# klucz_publiczny_DSA, klucz_prywatny_DSA = genDSA() +# print(klucz_publiczny_DSA, klucz_prywatny_DSA) +# p, q, g, y = klucz_publiczny_DSA +# x = klucz_prywatny_DSA +# +# m = "Thats my Kung Fu".encode("ASCII") +# print(m) +# r, s = sig_DSA(p, q, g, x, m) +# print(r, s) +# b = ver_DSA(p, q, g, y, m, r, s) +# print(b) diff --git a/klient.py b/klient.py index 43c493d..68bd5e1 100644 --- a/klient.py +++ b/klient.py @@ -2,20 +2,57 @@ # echo-client.py import socket -HOST = "150.254.79.26" +from dsa import ver_DSA + +byteorder = "big" +HOST = "150.254.79.244" PORT = 65432 # The port used by the server -with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.connect((HOST, PORT)) - klucz_publiczny = s.recv(16384) - print(klucz_publiczny) - do_podpisania = b"Hello, world" - s.sendall(do_podpisania) - podpis = s.recv(4096) +print() +print("-"*200) +print("tu sie zaczyna program klient.py") + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket1: + socket1.connect((HOST, PORT)) + + hello = socket1.recv(1024) + print(hello) + + p_len = socket1.recv(1024) + q_len = socket1.recv(1024) + g_len = socket1.recv(1024) + y_len = socket1.recv(1024) + + print(int.from_bytes(p_len, byteorder),int.from_bytes(q_len, byteorder),int.from_bytes(g_len, byteorder),int.from_bytes(y_len, byteorder)) + + p = socket1.recv(int.from_bytes(p_len, byteorder)) + q = socket1.recv(int.from_bytes(q_len, byteorder)) + g = socket1.recv(int.from_bytes(g_len, byteorder)) + y = socket1.recv(int.from_bytes(y_len, byteorder)) + + int_p = int.from_bytes(p, byteorder) + int_q = int.from_bytes(q, byteorder) + int_g = int.from_bytes(g, byteorder) + int_y = int.from_bytes(y, byteorder) + + print(int_p,int_q,int_g,int_y) + + do_podpisania = b"Hello, world" + socket1.sendall(do_podpisania) + + r_len = socket1.recv(1024) + s_len = socket1.recv(1024) + + print(int.from_bytes(r_len, byteorder), int.from_bytes(s_len, byteorder)) + + r = socket1.recv(int.from_bytes(r_len, byteorder)) + s = socket1.recv(int.from_bytes(s_len, byteorder)) + + int_r = int.from_bytes(r, byteorder) + int_s = int.from_bytes(s, byteorder) + + print(int_r,int_s) -print(f"Received {podpis}") -p, q, g, y = klucz_publiczny_DSA m = do_podpisania -r, s = podpis -weryfikacja_DSA = ver_DSA(p, q, g, y, m, r, s) +weryfikacja_DSA = ver_DSA(int_p, int_q, int_g, int_y, m, int_r,int_s) print(weryfikacja_DSA) diff --git a/main.py b/main.py index f535c7e..83d7c74 100644 --- a/main.py +++ b/main.py @@ -1,75 +1,75 @@ -# r_podwojne = b"01001001001001000011101101101111011110" - -N = 8 -# r od n -r1 = b"11111111" -r2 = b"11111111" -r1 = (int(r1, 2)) + (1 << N) -r2 = (int(r2, 2)) + (1 << N) -# t od 1 -t1 = b"10001110" -t2 = b"10010101" -t1 = (int(t1, 2)) + (1 << N) -t2 = (int(t2, 2)) + (1 << N) +# # r_podwojne = b"01001001001001000011101101101111011110" # -pdpkt_a = b"000011" -pdkpt_b = b"101001" -pdpkt_a = (int(pdpkt_a, 2)) + (1 << N) -pdkpt_b = (int(pdkpt_b, 2)) + (1 << N) - -def nastepny_bit(r, t): - rc = r - tc = t - wyn = None - #print(bin(rc)) - #print(bin(tc)) - for i in range(N): - bit = rc & 1 - bit_z_t = tc & 1 - if bit_z_t == 1: - if wyn == None: - wyn = bit - else: - wyn = wyn ^ bit - rc = rc >> 1 - tc = tc >> 1 - #print(bit) - #print(wyn) - #print(bin(rc)) - #print(bin(tc)) - #print("xor", bin(wyn)) - wywalane_z_r = r & 1 - #print("r", bin(r)) - r = r >> 1 - #print("podzielone", bin(r)) - r = r - (1 << N - 1) - r = r + (wyn << N - 1) - r = r + (1 << N) - #print("r dodane", bin(r)) - return r, wywalane_z_r - - -#r1, b = nastepny_bit(r1, t1) -#print(bin(r1)) -#print(b) - - -def shrinking_generator(r1, r2, t1, t2,rundy=10000): - wyn = [] - for i in range(rundy): - r1, l1 = nastepny_bit(r1, t1) - r2, l2 = nastepny_bit(r2, t2) - if l1 == 1: - wyn.append(l2) - print(l2, end="") - if i>0 and i%500==0: - print() - print() - return wyn - -wyn = shrinking_generator(r1,r2,t1,t2) -print(len(wyn)) - -wyn = shrinking_generator(pdpkt_a,pdkpt_b,pdpkt_a,pdkpt_b) -print(len(wyn)) -print(wyn) \ No newline at end of file +# N = 8 +# # r od n +# r1 = b"11111111" +# r2 = b"11111111" +# r1 = (int(r1, 2)) + (1 << N) +# r2 = (int(r2, 2)) + (1 << N) +# # t od 1 +# t1 = b"10001110" +# t2 = b"10010101" +# t1 = (int(t1, 2)) + (1 << N) +# t2 = (int(t2, 2)) + (1 << N) +# # +# pdpkt_a = b"000011" +# pdkpt_b = b"101001" +# pdpkt_a = (int(pdpkt_a, 2)) + (1 << N) +# pdkpt_b = (int(pdkpt_b, 2)) + (1 << N) +# +# def nastepny_bit(r, t): +# rc = r +# tc = t +# wyn = None +# #print(bin(rc)) +# #print(bin(tc)) +# for i in range(N): +# bit = rc & 1 +# bit_z_t = tc & 1 +# if bit_z_t == 1: +# if wyn == None: +# wyn = bit +# else: +# wyn = wyn ^ bit +# rc = rc >> 1 +# tc = tc >> 1 +# #print(bit) +# #print(wyn) +# #print(bin(rc)) +# #print(bin(tc)) +# #print("xor", bin(wyn)) +# wywalane_z_r = r & 1 +# #print("r", bin(r)) +# r = r >> 1 +# #print("podzielone", bin(r)) +# r = r - (1 << N - 1) +# r = r + (wyn << N - 1) +# r = r + (1 << N) +# #print("r dodane", bin(r)) +# return r, wywalane_z_r +# +# +# #r1, b = nastepny_bit(r1, t1) +# #print(bin(r1)) +# #print(b) +# +# +# def shrinking_generator(r1, r2, t1, t2,rundy=10000): +# wyn = [] +# for i in range(rundy): +# r1, l1 = nastepny_bit(r1, t1) +# r2, l2 = nastepny_bit(r2, t2) +# if l1 == 1: +# wyn.append(l2) +# print(l2, end="") +# if i>0 and i%500==0: +# print() +# print() +# return wyn +# +# wyn = shrinking_generator(r1,r2,t1,t2) +# print(len(wyn)) +# +# wyn = shrinking_generator(pdpkt_a,pdkpt_b,pdpkt_a,pdkpt_b) +# print(len(wyn)) +# print(wyn) \ No newline at end of file diff --git a/rsa.py b/rsa.py index 22cca90..92abb4f 100644 --- a/rsa.py +++ b/rsa.py @@ -265,18 +265,18 @@ def deszyfrowanie_RSA_OAEP_efektywne(N, d, c, p, q): return m -klucz_publiczny_RSA, klucz_prywatny_RSA, p, q = ( - 519761468615543605427347715703470532429938244355530617462517836775139947735532430649086044712389065327373881495341570956912263636228899450218205333731994709572385570226639327413165593530360209805759468603639857045256463923468775944877723831073218948735316969800684547536389344549931456857234306842372788643144370170015431494005792115059852604376019742040737720145831855341361995847454762357088678899180062704532000519408988445258760142165454104557986566507437171014234130588907895586535673103026294670410052684946543540355295153040713047596704353948168657913426604838668582082688505569615845354438639297748571125719945947883870935481915168118357450909437162890949276124288629273158626408730136153353280259385734094248099908877393332627830019602495378740189414275791739220123642063963148205857703085745460786497856110396863143452962185817050131451659771071779171468538379804253272573761890205034195675788656368202533040838395434243520115268109258521563259069746861504072033382067677548531381312853098367831348608517017347561078932501869038526817662345324458956129880592307734774686126383765519554521082281355928596367373896420049772278195723106268205519834478036673827492711851824231074849054234160240213942064673753130040021561496201, - 477381706395942701152074095578632486629565400458439476605265085853794121036702612260159616584121387309492215289565364823820329641787576027356541944084504070937525199582998228494161581256587963350479182569767709073476305942938578516914185238354982945951325610464183760057011556336116027667431478443971789852497566022190931964624926914512000465438808202901732238917670859079331357158287446481486109341772535642821404724625886765680916122532309926171425411343129633761470730490722435328973775404176016783739955436887938755254368532747248553601029668098763389748002919821495769996090735951060509426001668094204167013619340151559257016228800996225376949328480285921295583758343588892868414107256524587102802726669931221977934082695445776450539968355018778177567062955955188598370628274906129902341525667948146220455560650547385726862123568732001996709039776914328180353694837345124024464856436018138531057521237527151154874694659679915931307604482959674167927057989516514567590062270287113788435548631219164690228588947375243868674913030414096170014455884449983476702938473784682164922729359739421419438500787506002570332539986745630166359816334300274692876831789517660550946290972807224515579551609831535687760126827024718122063108706599), 351666562595755180375395684706241055066156015118305238714672681051882280187504041643434000791814441548607213636968471872699749514579264747751265631272256072725451071040601318567317130676266378472799425651993837557182657388293299120738132040687844779805705782033358582690112156036725659897429182523487027733035393136127607866012534194708593248355973043622949956480835065663676049584885243751590721741501965828757835520710448143589654198570646114663285241248290713467857410885600973553990582698560020712556891679552160547216847450959753723375562391525266281121596206287835239825464988412752920851291059488855778730214384761750487421888963628985845627716345194071713463878260961723623247223927256636464995367615914339908375807571549256908577125462826282135037163236879694999772105827399413764752262353954388137128091790174996148478228118409615917295349567618090677194660136786354642358341440475271809799562062575133857219513963659279640385931136925337147793582596685284559585465388622433797482322409875254412068070757284707921223037714540415540426945620781055610759002438036137835981012468827444588505213686787128969359012972104378460526475214473144375243797989638951510244270960646393807997876101642432170306423462852210882508497178399, 25135373776665590901202336545666836840803391456241601649197796217462555674052551470602221560430959564950903058760936416369952872220340695429584839910539112417172641186658957618681049568598438406515601629945861509808185343104246257658988353826830801833070838727357392230011787930325906736489581750024881433299826206439672753310114100220662748513681137812457302883579006216830084552049499018274245345348857361546260882853255668574117727827869922237413735566854945952091470823515266658679062817428979342500513620275531312300114575338687577827028450767856524439798014817991240510086446185465180093752648368250982692996851, 20678485756120477969749679171411892295363660397911095536909078058504743163718110463975943096121495385121226766215138672406087647843042984392942673880078680348165649479590165596137059916732118274875699928110613763860604584697412776630385407698849636163585027765534352995283408392656841447656433417025991247442767235071030016493820494123404672276837056547392384047230316759182798179296559194938230590081605300708541927122089496849298160531622588496007433560523950635471318918140715059942293286734964890852647775755004955456829006947025556582805868127595216838587483482482814544235364358121943295803510671636196385576851 -klucz_publiczny_RSA,klucz_prywatny_RSA, p, q = genRSA() -print(klucz_publiczny_RSA, klucz_prywatny_RSA, p, q) -N, e = klucz_publiczny_RSA -d = klucz_prywatny_RSA -m = "Thats my Kung Fu".encode("ASCII") -c = szyfrowanie_RSA_OAEP(N, e, m) -print("szyfrgr", c) -m = deszyfrowanie_RSA_OAEP(N, d, c) -print(m) -m = deszyfrowanie_RSA_OAEP_efektywne(N, d, c, p, q) -print(m) +# klucz_publiczny_RSA, klucz_prywatny_RSA, p, q = ( +# 519761468615543605427347715703470532429938244355530617462517836775139947735532430649086044712389065327373881495341570956912263636228899450218205333731994709572385570226639327413165593530360209805759468603639857045256463923468775944877723831073218948735316969800684547536389344549931456857234306842372788643144370170015431494005792115059852604376019742040737720145831855341361995847454762357088678899180062704532000519408988445258760142165454104557986566507437171014234130588907895586535673103026294670410052684946543540355295153040713047596704353948168657913426604838668582082688505569615845354438639297748571125719945947883870935481915168118357450909437162890949276124288629273158626408730136153353280259385734094248099908877393332627830019602495378740189414275791739220123642063963148205857703085745460786497856110396863143452962185817050131451659771071779171468538379804253272573761890205034195675788656368202533040838395434243520115268109258521563259069746861504072033382067677548531381312853098367831348608517017347561078932501869038526817662345324458956129880592307734774686126383765519554521082281355928596367373896420049772278195723106268205519834478036673827492711851824231074849054234160240213942064673753130040021561496201, +# 477381706395942701152074095578632486629565400458439476605265085853794121036702612260159616584121387309492215289565364823820329641787576027356541944084504070937525199582998228494161581256587963350479182569767709073476305942938578516914185238354982945951325610464183760057011556336116027667431478443971789852497566022190931964624926914512000465438808202901732238917670859079331357158287446481486109341772535642821404724625886765680916122532309926171425411343129633761470730490722435328973775404176016783739955436887938755254368532747248553601029668098763389748002919821495769996090735951060509426001668094204167013619340151559257016228800996225376949328480285921295583758343588892868414107256524587102802726669931221977934082695445776450539968355018778177567062955955188598370628274906129902341525667948146220455560650547385726862123568732001996709039776914328180353694837345124024464856436018138531057521237527151154874694659679915931307604482959674167927057989516514567590062270287113788435548631219164690228588947375243868674913030414096170014455884449983476702938473784682164922729359739421419438500787506002570332539986745630166359816334300274692876831789517660550946290972807224515579551609831535687760126827024718122063108706599), 351666562595755180375395684706241055066156015118305238714672681051882280187504041643434000791814441548607213636968471872699749514579264747751265631272256072725451071040601318567317130676266378472799425651993837557182657388293299120738132040687844779805705782033358582690112156036725659897429182523487027733035393136127607866012534194708593248355973043622949956480835065663676049584885243751590721741501965828757835520710448143589654198570646114663285241248290713467857410885600973553990582698560020712556891679552160547216847450959753723375562391525266281121596206287835239825464988412752920851291059488855778730214384761750487421888963628985845627716345194071713463878260961723623247223927256636464995367615914339908375807571549256908577125462826282135037163236879694999772105827399413764752262353954388137128091790174996148478228118409615917295349567618090677194660136786354642358341440475271809799562062575133857219513963659279640385931136925337147793582596685284559585465388622433797482322409875254412068070757284707921223037714540415540426945620781055610759002438036137835981012468827444588505213686787128969359012972104378460526475214473144375243797989638951510244270960646393807997876101642432170306423462852210882508497178399, 25135373776665590901202336545666836840803391456241601649197796217462555674052551470602221560430959564950903058760936416369952872220340695429584839910539112417172641186658957618681049568598438406515601629945861509808185343104246257658988353826830801833070838727357392230011787930325906736489581750024881433299826206439672753310114100220662748513681137812457302883579006216830084552049499018274245345348857361546260882853255668574117727827869922237413735566854945952091470823515266658679062817428979342500513620275531312300114575338687577827028450767856524439798014817991240510086446185465180093752648368250982692996851, 20678485756120477969749679171411892295363660397911095536909078058504743163718110463975943096121495385121226766215138672406087647843042984392942673880078680348165649479590165596137059916732118274875699928110613763860604584697412776630385407698849636163585027765534352995283408392656841447656433417025991247442767235071030016493820494123404672276837056547392384047230316759182798179296559194938230590081605300708541927122089496849298160531622588496007433560523950635471318918140715059942293286734964890852647775755004955456829006947025556582805868127595216838587483482482814544235364358121943295803510671636196385576851 +# klucz_publiczny_RSA,klucz_prywatny_RSA, p, q = genRSA() +# print(klucz_publiczny_RSA, klucz_prywatny_RSA, p, q) +# N, e = klucz_publiczny_RSA +# d = klucz_prywatny_RSA +# m = "Thats my Kung Fu".encode("ASCII") +# c = szyfrowanie_RSA_OAEP(N, e, m) +# print("szyfrgr", c) +# m = deszyfrowanie_RSA_OAEP(N, d, c) +# print(m) +# m = deszyfrowanie_RSA_OAEP_efektywne(N, d, c, p, q) +# print(m) diff --git a/serwer.py b/serwer.py index 4e32e46..bb3b491 100644 --- a/serwer.py +++ b/serwer.py @@ -1,15 +1,22 @@ # https://realpython.com/python-sockets/ # echo-server.py import socket +import sys +from dsa import sig_DSA + +byteorder = "big" PORT = 65432 # Port to listen on (non-privileged ports are > 1023) ip_info = socket.getaddrinfo(socket.gethostname(), PORT) # print(ip_info) HOST = ip_info[-1][-1][0] +print() +print("-"*200) +print("tu sie zaczyna program serwer.py") print(HOST, ":", PORT) -klucz_publiczny_DSA, klucz_prywatny_DSA = (126220152782999491712146716573647923575210663342203877875997463151103734117618780085961998496900968641688295271738389523168262437046367952410100230715977561698699865420228930481833546803155826687224437601550687821059803843460614018330735329864730711512369415813709177511423225057391814747172616206699791431113, 1026153776632400528836961826077414494924816203371, 33135294496110223909298149780807071663246150710874084366253067988796622596843824622279842916377597978486097816392781318387311279773566894876369811406237208448656803682244786284714051489351162528336310611509464319257937304463246532853715133677606472539203807274116025362212599140301963520366587373345549232664, 57789326082328055343244183952945963065038001008054371988423611501774965584951836084636408462704147650194354237953178434916234787575914934345509256412623906774868805317306029570637051066904576874737549979887847806955662502738986630454336931791238784800404714101190106940899556139200869987149012959023189160975) 601201094732353224640534306619895007746271908982 +klucz_publiczny_DSA, klucz_prywatny_DSA = (126220152782999491712146716573647923575210663342203877875997463151103734117618780085961998496900968641688295271738389523168262437046367952410100230715977561698699865420228930481833546803155826687224437601550687821059803843460614018330735329864730711512369415813709177511423225057391814747172616206699791431113, 1026153776632400528836961826077414494924816203371, 33135294496110223909298149780807071663246150710874084366253067988796622596843824622279842916377597978486097816392781318387311279773566894876369811406237208448656803682244786284714051489351162528336310611509464319257937304463246532853715133677606472539203807274116025362212599140301963520366587373345549232664, 57789326082328055343244183952945963065038001008054371988423611501774965584951836084636408462704147650194354237953178434916234787575914934345509256412623906774868805317306029570637051066904576874737549979887847806955662502738986630454336931791238784800404714101190106940899556139200869987149012959023189160975), 601201094732353224640534306619895007746271908982 p, q, g, y = klucz_publiczny_DSA x = klucz_prywatny_DSA @@ -18,11 +25,27 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.listen() conn, addr = s.accept() conn.send(b"klucz publiczny") - conn.sendall(klucz_publiczny_DSA) + + conn.send(p.bit_length().to_bytes(1024, byteorder)) + conn.send(q.bit_length().to_bytes(1024, byteorder)) + conn.send(g.bit_length().to_bytes(1024, byteorder)) + conn.send(y.bit_length().to_bytes(1024, byteorder)) + + conn.sendall(p.to_bytes(p.bit_length()+7//8, byteorder)) + conn.sendall(q.to_bytes(q.bit_length()+7//8, byteorder)) + conn.sendall(g.to_bytes(g.bit_length()+7//8, byteorder)) + conn.sendall(y.to_bytes(y.bit_length()+7//8, byteorder)) + with conn: print(f"Connected by {addr}") while True: - data = conn.recv(1024) - if not data: + do_podpisania = conn.recv(1024) + if not do_podpisania: break - conn.sendall(data) + m = do_podpisania + r, s = sig_DSA(p, q, g, x, m) + conn.send(r.bit_length().to_bytes(1024, byteorder)) + conn.send(s.bit_length().to_bytes(1024, byteorder)) + + conn.sendall(r.to_bytes(r.bit_length() + 7 // 8, byteorder)) + conn.sendall(s.to_bytes(s.bit_length() + 7 // 8, byteorder))