mirror of
https://github.com/20kaushik02/CSE548_ACNS_Work.git
synced 2025-12-06 05:24:07 +00:00
let's start 1.3
This commit is contained in:
parent
ea18bb56ef
commit
4ce1b6611a
209
hw1.3/ratchet/mallory.py
Normal file
209
hw1.3/ratchet/mallory.py
Normal file
@ -0,0 +1,209 @@
|
||||
# mallory.py, a mitm attack to learn ratchet gossip about your CSE 539 instructor
|
||||
# jedimaestro@asu.edu
|
||||
|
||||
import secrets
|
||||
import sys
|
||||
import os.path
|
||||
import time
|
||||
import os
|
||||
import re
|
||||
from colorama import Fore, Back, Style
|
||||
import signal
|
||||
|
||||
g = 10697987386528634556132351984988679277669693771233806587823166397646257882356751154924044166012048802718827287365490918762116743604708315476073224232944329
|
||||
|
||||
p = 21395974773057269112264703969977358555339387542467613175646332795292515764713502309848088332024097605437654574730981837524233487209416630952146448465888659
|
||||
|
||||
if (len(sys.argv) < 3) or sys.argv[1] == sys.argv[2]:
|
||||
print("No! RTFSC!")
|
||||
exit(0)
|
||||
|
||||
flog = open("mallorylog.txt", "w")
|
||||
|
||||
myname = sys.argv[1]
|
||||
theirname = sys.argv[2]
|
||||
|
||||
imfirst = myname > theirname
|
||||
if imfirst:
|
||||
fn = "/tmp/" + myname + "-and-" + theirname + ".txt"
|
||||
else:
|
||||
fn = "/tmp/" + theirname + "-and-" + myname + ".txt"
|
||||
#if os.path.isfile(fn):
|
||||
# os.remove(fn)
|
||||
|
||||
def handler(signum, frame):
|
||||
print(Style.RESET_ALL + "Ctrl-c was pressed. Exiting...")
|
||||
exit(0)
|
||||
|
||||
|
||||
|
||||
signal.signal(signal.SIGINT, handler)
|
||||
|
||||
messagenumber = 6
|
||||
leakerslatestratchet = 0
|
||||
otherslatestratchet = 0
|
||||
mylatestprivatekeyforleaker = 0
|
||||
mylatestprivatekeyforother = 0
|
||||
|
||||
def otp(plainorcipher, encordec, key):
|
||||
# plainorcipher is the text for plaintext or ciphertext
|
||||
# encordec is "enc" to encrypt or "dec" to decrypt
|
||||
# key is a large integer with hopefully enough key material
|
||||
if encordec == "enc":
|
||||
sign = 1
|
||||
elif encordec == "dec":
|
||||
sign = -1
|
||||
else:
|
||||
return "Bad use of bad API"
|
||||
keystream = []
|
||||
ki = key
|
||||
while ki > 0:
|
||||
keystream.append(ki % 26)
|
||||
ki = int(ki / 26)
|
||||
whattoreturn = ""
|
||||
for i in range(len(plainorcipher)):
|
||||
letter = ord(plainorcipher[i]) - ord('A')
|
||||
c = (letter + keystream[i]*sign) % 26
|
||||
c = c + ord('A')
|
||||
whattoreturn = whattoreturn + str(chr(c))
|
||||
return whattoreturn
|
||||
|
||||
olderlatestratchet = 0
|
||||
olderciphertext = ""
|
||||
ciphertext = ""
|
||||
|
||||
while not os.path.isfile("/tmp/oops.txt"):
|
||||
if os.path.isfile(fn):
|
||||
fin = open(fn, "r")
|
||||
if fin:
|
||||
newotherslatestratchet = int(fin.readline())
|
||||
if otherslatestratchet != newotherslatestratchet:
|
||||
olderlatestratchet = otherslatestratchet
|
||||
otherslatestratchet = newotherslatestratchet
|
||||
flog.write("ratchet")
|
||||
flog.write("\n")
|
||||
flog.write(str(otherslatestratchet))
|
||||
flog.write("\n")
|
||||
newciphertext = fin.readline().rstrip('\n')
|
||||
if ciphertext != newciphertext:
|
||||
olderciphertext = ciphertext
|
||||
ciphertext = newciphertext
|
||||
flog.write("ciphertext")
|
||||
flog.write("\n")
|
||||
flog.write(ciphertext)
|
||||
flog.write("\n")
|
||||
fin.close()
|
||||
time.sleep(0.3)
|
||||
oops = open("/tmp/oops.txt", "r")
|
||||
mylatestprivatekeyforother = int(oops.readline().rstrip('\n'))
|
||||
oops.close()
|
||||
os.remove("/tmp/oops.txt")
|
||||
|
||||
fin = open(fn, "r")
|
||||
leakerslatestratchet = int(fin.readline())
|
||||
newerciphertext = fin.readline().rstrip('\n')
|
||||
fin.close()
|
||||
|
||||
# We can decrypt one message already
|
||||
s = pow(otherslatestratchet, mylatestprivatekeyforother, p)
|
||||
plaintext = otp(newerciphertext, "dec", s)
|
||||
print("Eavesdropped " + theirname + ": " + plaintext)
|
||||
|
||||
flog.write("stolenkey")
|
||||
flog.write("\n")
|
||||
flog.write(str(mylatestprivatekeyforother))
|
||||
flog.write("\n")
|
||||
|
||||
|
||||
while True:
|
||||
# Wait for the other to update the file
|
||||
while os.path.isfile(fn):
|
||||
time.sleep(0.001)
|
||||
while not os.path.isfile(fn):
|
||||
time.sleep(0.001)
|
||||
time.sleep(0.3)
|
||||
# Read the info and delete the file
|
||||
fin = open(fn, "r")
|
||||
theline = fin.readline()
|
||||
#print(theline)
|
||||
otherslatestratchet = int(theline)
|
||||
ciphertext = fin.readline().rstrip('\n')
|
||||
fin.close()
|
||||
os.remove(fn)
|
||||
# Do part of a key exchange with other, decrypt if possible
|
||||
s = pow(otherslatestratchet, mylatestprivatekeyforother, p)
|
||||
plaintext = otp(ciphertext, "dec", s)
|
||||
flog.write("ciphertextforleaker")
|
||||
flog.write("\n")
|
||||
flog.write(ciphertext)
|
||||
flog.write("\n")
|
||||
print("Eavesdropped " + myname + ": " + plaintext)
|
||||
# Do a key exchange with leaker
|
||||
mylatestprivatekeyforleaker = secrets.randbelow(p)
|
||||
mylatestprivatekeyforother = secrets.randbelow(p)
|
||||
flog.write("keys")
|
||||
flog.write("\n")
|
||||
flog.write(str(mylatestprivatekeyforleaker))
|
||||
flog.write("\n")
|
||||
flog.write(str(mylatestprivatekeyforother))
|
||||
flog.write("\n")
|
||||
myratchetforleaker = pow(g, mylatestprivatekeyforleaker, p)
|
||||
s = pow(leakerslatestratchet, mylatestprivatekeyforleaker, p)
|
||||
# Reencrypt and make file
|
||||
ciphertext = otp(plaintext, "enc", s)
|
||||
fout = open(fn, "w")
|
||||
flog.write("ratchetsleaker")
|
||||
flog.write("\n")
|
||||
flog.write(str(leakerslatestratchet))
|
||||
flog.write("\n")
|
||||
fout.write(str(myratchetforleaker))
|
||||
fout.write("\n")
|
||||
fout.write(ciphertext)
|
||||
fout.write("\n")
|
||||
fout.flush()
|
||||
fout.close()
|
||||
# Wait for the leaker to update the file
|
||||
while os.path.isfile(fn):
|
||||
time.sleep(0.001)
|
||||
while not os.path.isfile(fn):
|
||||
time.sleep(0.001)
|
||||
time.sleep(0.3)
|
||||
# Read the info and delete the file
|
||||
fin = open(fn, "r")
|
||||
theline = fin.readline()
|
||||
#print(theline)
|
||||
leakerslatestratchet = int(theline)
|
||||
ciphertext = fin.readline().rstrip('\n')
|
||||
fin.close()
|
||||
os.remove(fn)
|
||||
# Do a key exchange with other, decrypt if possible
|
||||
myratchetforother = pow(g, mylatestprivatekeyforother, p)
|
||||
s = pow(leakerslatestratchet, mylatestprivatekeyforleaker, p)
|
||||
plaintext = otp(ciphertext, "dec", s)
|
||||
flog.write("ciphertextforother")
|
||||
flog.write("\n")
|
||||
flog.write(ciphertext)
|
||||
flog.write("\n")
|
||||
print("Eavesdropped " + theirname + ": " + plaintext)
|
||||
# Reencrypt and make file
|
||||
s = pow(otherslatestratchet, mylatestprivatekeyforother, p)
|
||||
ciphertext = otp(plaintext, "enc", s)
|
||||
fout = open(fn, "w")
|
||||
flog.write("ratchetsother")
|
||||
flog.write("\n")
|
||||
flog.write(str(otherslatestratchet))
|
||||
flog.write("\n")
|
||||
fout.write(str(myratchetforother))
|
||||
fout.write("\n")
|
||||
fout.write(ciphertext)
|
||||
fout.write("\n")
|
||||
fout.flush()
|
||||
fout.close()
|
||||
messagenumber = messagenumber + 2
|
||||
if (False and messagenumber == 16):
|
||||
print("droppingtheball...")
|
||||
time.sleep(40)
|
||||
|
||||
|
||||
|
||||
|
||||
207
hw1.3/ratchet/mallorylog-3.txt
Normal file
207
hw1.3/ratchet/mallorylog-3.txt
Normal file
@ -0,0 +1,207 @@
|
||||
ratchet
|
||||
16725500843986780859290964241383377689984325029926842324245042174274897077197408155765476159770724364734641657781173489002677390786059065938693970053361057
|
||||
ratchet
|
||||
5242069083042441840139293145015105668941043342886718560897372375238472997785848511731788368786738329759063195317369221671153919277115558622928185763050217
|
||||
ciphertext
|
||||
HPQWQQBMTBYQGJMDQNILIIMZQRSPGCJFKZ
|
||||
ratchet
|
||||
18340249256681315960604486279559527229403804985364888315528904824786671045544062872321276835700555134349417889141666988622871098047412096806229255544731366
|
||||
ciphertext
|
||||
IIOJPMPIDCMYLYWVEJYKLEBZLATYKVYNUTDDNERMQBJKHYPOYJNEQNKHYABZYZQOMMSPMYJSXIEFZLGPEHYYQUSUHUY
|
||||
ratchet
|
||||
1790228137670895438812322122018891748753687773807511585361447121012119119881011783805564687702663436623344621025914938128827590360706784990912279039969362
|
||||
ciphertext
|
||||
AMSUXEWXMTCKOVWLQLUHEXZRAXUTXYSMY
|
||||
ratchet
|
||||
12847475081820443616693277352816870636852348895411123815272692589172122292390147287566741042754159062737658522891587402204655902513198096619671838173811631
|
||||
ciphertext
|
||||
GUHZOJQJSNEFFCHAGBGVUJGNZWIOBCHGGVFYFUKMAEMOTAUQFBZWTQNEUEBYKWQWRMOWHTVIGLNQIOW
|
||||
ratchet
|
||||
1900045637829144094469604781411622977934084157788606678211226373737958688079437592874701898766439234359324238679874902927424998973284533979951895848764342
|
||||
ciphertext
|
||||
AQEJSQOCMDFCTMBHSXOGIPLCAXXYOVQLSX
|
||||
stolenkey
|
||||
14636432677487680427691020376136988323477173259137562085361994851504349552093729628440722297515442012424868127866461609525527706197919833341816867775573102
|
||||
ciphertextforleaker
|
||||
GPYHXYPSNACWINXZKBKQIUDCW
|
||||
keys
|
||||
15746499221924466697632020508537852207275489660674221612465594041100176568471508671793215481007776581547311994745226296201151862598485359006574764120410698
|
||||
1091673800787212636612767598864527246860219492217415957273710927596680086630801672923254538217117838278408511024704026629967443407884251473354418686526997
|
||||
ratchetsleaker
|
||||
21107907938610721958429509363784217763391049266475165461258520116008641978015151699517662649590146322039043597148794687043290773049946313646553474753430345
|
||||
ciphertextforother
|
||||
JOTWRQQBADYXGNWFQPSMBCUMYLWKBRDENWTAGRXISTRSNAPINJBIFTOBCFAJCTIQIHTGPKNIADQRO
|
||||
ratchetsother
|
||||
16063763874924191707634541225536059129834113196645807222522102723439227523598956846045906796061922333884535846801583648991837971761941279549272330221274745
|
||||
ciphertextforleaker
|
||||
WKHXAQGOKWJLQBSRYKBZMRCJSJQV
|
||||
keys
|
||||
16807206118628667381951400878321655855391620856978852823865569248541980093095815139674611840723036731911053649790828312336413563276844360348140203074486113
|
||||
14733529546324596645308210296817256629975023662798537035188780133319721629014462385000649685768084769244804362833137841105703103240374646464638399916538301
|
||||
ratchetsleaker
|
||||
6424818556520927126597045343197883742968081188847027979794545169691598273769496707941129868930108429262527581433439486396375110205505072711046943991159194
|
||||
ciphertextforother
|
||||
EYHJYVENKWBYWIFHIWSGTENQMOAEWHAUYG
|
||||
ratchetsother
|
||||
5390006009827328168675903345712284832027356420136667109293505796159536380706519308705509687040801838397419750401030571510341695976507817857530204612744709
|
||||
ciphertextforleaker
|
||||
HTYMOTCIQKFQJC
|
||||
keys
|
||||
18876936488753724234417515518173304065477980337905012234486031004063726923912805635864879156224848420362058265392616097501467566053786106456615235745957549
|
||||
5486647552548306973205772603625927064084803550655663756551553017498272562656088836605647502785414192749799992541526863442797373650490930596626712238624996
|
||||
ratchetsleaker
|
||||
1798101015683717838795772149433371764074564379226653954873987034584838033246458110146152122712473454380438358532493789498016902489595862287686493475911134
|
||||
ciphertextforother
|
||||
NQJOFQGXJKLCVWNMLUFGNAWRGANWAKRKNNSMJTWFEJVIHIXEJAHOILOZKWX
|
||||
ratchetsother
|
||||
15497151221995556984369691407291504999145025102427719518820574044020482051015947001396233195254721832072918143968515743575878903583529490389881927631156543
|
||||
ciphertextforleaker
|
||||
EYRSYUKWEWJJYLSPAWAS
|
||||
keys
|
||||
18043763569234412684714233016263796138750338205829751940278667533526226111033103835056939382933546292561312325124183659901086585016234323998491192552407491
|
||||
3443354882545669452695694510427192825344468745506455417082818159960826133209889487686383085980562253910788578885127995888003341935505130718975633395234970
|
||||
ratchetsleaker
|
||||
12611832578647867843426861204583029720343936842281316900591754706988481143409804434606474083412731924032117018820421893294222923157424745215711139332286419
|
||||
ciphertextforother
|
||||
FWPSWTGQUVGASZZKPGHCJPKZJGIBWERXGPIYXE
|
||||
ratchetsother
|
||||
888396531074933109085112887443949943187410223697559880838510733962365216465430678501270728442458185375938485894507759207264873831938785033335231781633375
|
||||
ciphertextforleaker
|
||||
CIBZKOKSGOVXWBGVSTARWMOK
|
||||
keys
|
||||
5288930278738206287343353232894577726022520348082076941743686647064737433772438546048340158851781113129763848805679096641692659457813828562037234149996197
|
||||
19783378463183105387661060994862549079971408984443051806731175378638064357752464096611224598010471173817648174921317550575019987379005143399774532614687041
|
||||
ratchetsleaker
|
||||
1119791986503575585125734446840229315973559020700550834717242483861703761107296749719898496778748448473682139406942978478114119705372719779672343043115757
|
||||
ciphertextforother
|
||||
KIQHILQVOGHYUFICACILGIWWZNEEBTJWWYUEFONWQCMOBYQMJFXOAAQNN
|
||||
ratchetsother
|
||||
17965113382391334154628776902785005179079238724108956067485969922843472372986996126434729147219354481732012494875948392662983925387553800768584631620968499
|
||||
ciphertextforleaker
|
||||
WOHKGYSSWLRFKYMU
|
||||
keys
|
||||
20169405904596505099316231377703889019320483219494628778042125180319633262446676559227460376928475827201689202943293642552313683573366914102653569607393647
|
||||
18005033887503133368725503231914954602911529658755229705678696564678031726619551865139441294244729503950593733845357002566376343314190618701773269784249445
|
||||
ratchetsleaker
|
||||
15151765594273531099267253149378623985917473169728685398700687135605545656441002913592386065142484121688296754952564078350897984749980833841115557665014098
|
||||
ciphertextforother
|
||||
XELFSOMBHYBARNWBIGUQWUONCNNKDWUHOZAWRJYHQ
|
||||
ratchetsother
|
||||
18592391146545106519873954759830518270929314490472209653914218035989825175523917394609437124045143503463070108257521358764304606762483964703612704755246988
|
||||
ciphertextforleaker
|
||||
FTYYANIUYGDIBXFKIYC
|
||||
keys
|
||||
16154824429628924834388608894065821553046721094590779463027194126895799723160191128983246600864339570382447245318125082915750568401456864565237167840557081
|
||||
21390197296074030970266242942015531652438000194346439791597768529287622072803436268619188397322027423801621342086916716168979599532875743351360427840967281
|
||||
ratchetsleaker
|
||||
14055430202501311474455051650182283184565749390956065686078373194093310802058542545051949988835549027044052244039648828496758209327496113206962118365734066
|
||||
ciphertextforother
|
||||
GMJRVEHYKOCEDWCOWATKMRFWK
|
||||
ratchetsother
|
||||
7824024691792427896802181935403689899564216196138644515167820810337076148989248036524892701390263155716913952802157026608031605975530506557269731797968288
|
||||
ciphertextforleaker
|
||||
AITWPYDQFOPYOWMYFXDGMME
|
||||
keys
|
||||
327592160195393230341889989688821639380805771938594560327252966322396483160850525533396643358742293852849597707094828893625516277694019049650506844311624
|
||||
16150835621620268289440676504102601700160368198928981261379023356924980131799476900461169468882233515709849876830882612144238609090133744258640848390530761
|
||||
ratchetsleaker
|
||||
21199545924897386863337634664622531723777695839428175723862400869444565198073371263735808670166664924986804713031849825815818944631182888750018431191357452
|
||||
ciphertextforother
|
||||
SMXMLQVUGIYXALOPCWQ
|
||||
ratchetsother
|
||||
6352699698673492174291154604230978615366985838413432008243820133560814897318230966641147629582414251365040134631727790387874608158484456503997223663918286
|
||||
ciphertextforleaker
|
||||
FRQUNYSLYQAWKEFIFCYHXYF
|
||||
keys
|
||||
14305382909830717891000216545676341552371807977045506646658220390640988738913571676461634403723416439588501121433070359521104429343340617392022285157646718
|
||||
14744774724591852174347115548078319301839962461836642429462348888601787195094047026427078299732974502903127143964150457551336164311067427862806639494429795
|
||||
ratchetsleaker
|
||||
15609192624998924777338927352444842920201465698254053330102951061842341181689992035922155613971595549007510085022429960349532371332941358145842553065182832
|
||||
ciphertextforother
|
||||
JCYYHOEOOPKXIMXSFQEPUNZKQYMPRAGLXGZE
|
||||
ratchetsother
|
||||
19362223963295894250429870729350452971912947338220003094495890179575539409812751447729085062071299865552997827535018076396185501559869596749856579792468722
|
||||
ciphertextforleaker
|
||||
SDUAODAYMEOABCFSEBPMHZBYSGM
|
||||
keys
|
||||
4137708380292074331694548764780255846479770360462378176423782847678557716633347223962593170528068187129293965224946705949906055453920876869955777473925970
|
||||
18514407092933096763045004164449025166296597330948587611549396634685938786939840812068599022095630843057663345472785765080084727418780590320350634909735441
|
||||
ratchetsleaker
|
||||
10121650152451840009502901522423318409616880111359989801513720395736142042908893269897509176657458794297694073368932262134650136586189273057466303599664406
|
||||
ciphertextforother
|
||||
VCFTOOCCSCLCKQHAFLYJVHYTGCTNQKWSBEXTOEMCBCEM
|
||||
ratchetsother
|
||||
11663500104490452916270094684153842432434885876861631791376786005549006294262778642822037029370505160193859287552796345915846855193877126067588819768065664
|
||||
ciphertextforleaker
|
||||
LORDMYSYSKFHSJUJWAZBAJSOYK
|
||||
keys
|
||||
20556368891220362190895892502960498402917168193475106878360325282488718501841265350897338125634037698861804183814891013402145330334550212390130110552716602
|
||||
3960970388297608478622749140626481527748745515384462792479464171794434689727879089580029154188017897443644246504398123828002525433825617558490162515995388
|
||||
ratchetsleaker
|
||||
2359667490619758686637979988793765197808739173607752456443132510135313217329999104085324156213539598542837835242623657663401361952651650965515876264491753
|
||||
ciphertextforother
|
||||
KELZMKTKHIRCRKRIMKXBGGEQTGHUZLJKVDYJCG
|
||||
ratchetsother
|
||||
4548820471954604019628231242251692308778770022214071214001903696046090865774502335540816251886694537487438741872533168326210239510968961452270587081387870
|
||||
ciphertextforleaker
|
||||
ZSBISQJUQTQXCIYLYYAKLGTVBAUCA
|
||||
keys
|
||||
10496526991690032042088911822259329017384445920953444274714726032074642977851354713056462857847132423825578322795021979610972359504714992240005518725099552
|
||||
18940717639566014085592838673834065008912716037123494728232507223728260560646591230882219536855792578951827626257630806604432391352611350111492124757639646
|
||||
ratchetsleaker
|
||||
1229691777987689310535240264779748993769662290766491819243347564502299487954373491191166469738462009297863657504418906704896947495030006843714814113694425
|
||||
ciphertextforother
|
||||
NUDTKWOGHIVKQTZDABZEUP
|
||||
ratchetsother
|
||||
19419066822889387405878501018663136155613968606043309635882739193649767663385999309032097842609640422871302477921814798697686044932551552455905265884227631
|
||||
ciphertextforleaker
|
||||
ZTGASUCCBQTKVQFVKQGIDI
|
||||
keys
|
||||
654878039679708581864082175751532470091483368661642334077394484883609673244458613729135375939482449371712049946089110732165604487947441327710790871814257
|
||||
20959071506017395017871585784931315962786014638367068313176165088166885536941834077956935894437857788087221939812570951163391754757369916610685240255125075
|
||||
ratchetsleaker
|
||||
13734009597763592799696182857028193813093031150106659501862810394777210703313601814817161102721448910056326884637470969187711920394802790671037020667157965
|
||||
ciphertextforother
|
||||
SMCNQJIVKMTCYVAMEYGRLFWPBUWCMFMYBMDFZCISCJZ
|
||||
ratchetsother
|
||||
19161614866134927481134458844519709161960277516154463917696261199698903743173630541054880485812385041081753316938792787706626668508883131061359623450968773
|
||||
ciphertextforleaker
|
||||
WUEMXILDCEVAQRUZGKCFE
|
||||
keys
|
||||
16531937820068368968723188035003329249107742135502524520362186536574700226864233708838722290658031124417276643517872724892556388316014822612910976154557779
|
||||
11535485793024480429665121089535716387977296510672048244700386253068051865347852403749643856505725497090390928817162450530045208516692935793483743738419818
|
||||
ratchetsleaker
|
||||
2952021808157156832634451530888146701178803371567774463162791204604784932827987095522806898475873140886779477023302813665165478283662909600383976194396211
|
||||
ciphertextforother
|
||||
CWPYTULPZOQPDQWNFQXITXKVOHROOTKKOXCMYQBWPRRAEAVQPGUWTEH
|
||||
ratchetsother
|
||||
4811278246889601321559115146472798376266255384872726589372185201538219080092350617551952205800776858088260330242915745047537792278205021035413686318431237
|
||||
ciphertextforleaker
|
||||
BUPLUCUXMVNMZACQDQPJBQESO
|
||||
keys
|
||||
12594787680053129228764084769984201399980050344448326504935327483212413185468777775625263391446680555049708949644058874968289967099084492523895752287097422
|
||||
16482023115697771811902759828865415254594602798888260276013262926820836700810556174558133008430505835209788500691592457068924793035759211393027373200622855
|
||||
ratchetsleaker
|
||||
16033097375518533144156206797693823151068585702752517346006662223897971268964649659276313233712810709336978540232487637046654754185460135374263288301014079
|
||||
ciphertextforother
|
||||
RKDBQEKLXPAHXWKCJJZBCHROIJKKKUHBLJYXMEW
|
||||
ratchetsother
|
||||
10334713464494709118421649170183720479672635798933466658805323968421944215308440772937465245473390813410431842644588045650549086951692410831670341411493373
|
||||
ciphertextforleaker
|
||||
LYZLWDIKTGHXCHIQPSSCWGMGXBOXBAW
|
||||
keys
|
||||
13079410331758372691821052682674979488465495616417149097162677713242167256543775864244096912455150034741067842777231504530547771763653965086981141981597471
|
||||
213339620293115389578666205490118843156504258574968386467279100611181524830109587551560973054314591282246221819054197455038391314030263065753935823779542
|
||||
ratchetsleaker
|
||||
11021794516434669480450729035659261334580071488599056875030005286802581711470911612589270983046212028345317390673049278989516822145122526902843715635905051
|
||||
ciphertextforother
|
||||
LQDWYXYIVYHCFKOGKSIVGRQPAPVLIZWIVXXKEFW
|
||||
ratchetsother
|
||||
478393942604435301890794729766445921449031654630030707990015924098111346476388094134804928338782867752787440831842627050734920592648807597272558244544396
|
||||
ciphertextforleaker
|
||||
EIJVOYASVA
|
||||
keys
|
||||
17028509889608467202760601350541455924182595341977255717719789153497309401872519593593301498789304180581191931640216728376624594909486992255970857588789240
|
||||
14300098506993097354291341486022017455499857448220079905334870651909810834776862156420529422913447181273003002623969166527512233564359169002312638822819984
|
||||
ratchetsleaker
|
||||
19423854541342631079257971474024571375486931730843549463539234413497022894596713664430242639894031247401438256084610307987099143073725749050219185210402917
|
||||
118
hw1.3/ratchet/ratchetness.py
Normal file
118
hw1.3/ratchet/ratchetness.py
Normal file
@ -0,0 +1,118 @@
|
||||
# ratchetness.py, a simplified form of OTR for spreading ratchet gossip about your CSE 539 instructor
|
||||
# jedimaestro@asu.edu
|
||||
|
||||
import secrets
|
||||
import sys
|
||||
import os.path
|
||||
import time
|
||||
import os
|
||||
import re
|
||||
from colorama import Fore, Back, Style
|
||||
import signal
|
||||
|
||||
g = 10697987386528634556132351984988679277669693771233806587823166397646257882356751154924044166012048802718827287365490918762116743604708315476073224232944329
|
||||
|
||||
p = 21395974773057269112264703969977358555339387542467613175646332795292515764713502309848088332024097605437654574730981837524233487209416630952146448465888659
|
||||
|
||||
if (len(sys.argv) < 3) or sys.argv[1] == sys.argv[2]:
|
||||
print("No! RTFSC!")
|
||||
exit(0)
|
||||
|
||||
myname = sys.argv[1]
|
||||
theirname = sys.argv[2]
|
||||
|
||||
imfirst = myname > theirname
|
||||
if imfirst:
|
||||
fn = "/tmp/" + myname + "-and-" + theirname + ".txt"
|
||||
else:
|
||||
fn = "/tmp/" + theirname + "-and-" + myname + ".txt"
|
||||
if imfirst:
|
||||
if os.path.isfile(fn):
|
||||
os.remove(fn)
|
||||
elif False:
|
||||
while os.path.isfile(fn):
|
||||
time.sleep(1)
|
||||
|
||||
def handler(signum, frame):
|
||||
print(Style.RESET_ALL + "Ctrl-c was pressed. Exiting...")
|
||||
if os.path.isfile(fn):
|
||||
os.remove(fn)
|
||||
exit(0)
|
||||
|
||||
|
||||
|
||||
signal.signal(signal.SIGINT, handler)
|
||||
|
||||
messagenumber = 0
|
||||
theirlatestratchet = 0
|
||||
mylatestprivatekey = 0
|
||||
|
||||
def otp(plainorcipher, encordec, key):
|
||||
# plainorcipher is the text for plaintext or ciphertext
|
||||
# encordec is "enc" to encrypt or "dec" to decrypt
|
||||
# key is a large integer with hopefully enough key material
|
||||
if encordec == "enc":
|
||||
sign = 1
|
||||
elif encordec == "dec":
|
||||
sign = -1
|
||||
else:
|
||||
return "Bad use of bad API"
|
||||
keystream = []
|
||||
ki = key
|
||||
while ki > 0:
|
||||
keystream.append(ki % 26)
|
||||
ki = int(ki / 26)
|
||||
whattoreturn = ""
|
||||
for i in range(len(plainorcipher)):
|
||||
letter = ord(plainorcipher[i]) - ord('A')
|
||||
c = (letter + keystream[i]*sign) % 26
|
||||
c = c + ord('A')
|
||||
whattoreturn = whattoreturn + str(chr(c))
|
||||
return whattoreturn
|
||||
|
||||
|
||||
while True:
|
||||
if (imfirst and messagenumber % 2 == 0) or ((not imfirst) and messagenumber % 2 == 1):
|
||||
mylatestprivatekey = secrets.randbelow(p)
|
||||
myratchet = pow(g, mylatestprivatekey, p)
|
||||
s = pow(theirlatestratchet, mylatestprivatekey, p)
|
||||
print(Fore.GREEN + "Sending... " + str(messagenumber))
|
||||
if messagenumber != 0:
|
||||
print(Fore.GREEN + "Please type something juicy... ")
|
||||
ratchetgossip = sys.stdin.readline().rstrip('\n')
|
||||
if len(ratchetgossip) == 0:
|
||||
exit(0)
|
||||
plaintext = re.sub(r'[^A-Z]', '', ratchetgossip.upper())
|
||||
ciphertext = otp(plaintext, "enc", s)
|
||||
|
||||
fout = open(fn, "w")
|
||||
fout.write(str(myratchet))
|
||||
fout.write("\n")
|
||||
if messagenumber != 0:
|
||||
fout.write(ciphertext)
|
||||
fout.write("\n")
|
||||
fout.flush()
|
||||
fout.close()
|
||||
messagenumber = messagenumber + 1
|
||||
while os.path.isfile(fn):
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
print(Fore.RED + "Receiving... " + str(messagenumber))
|
||||
while not os.path.isfile(fn):
|
||||
time.sleep(1)
|
||||
time.sleep(1)
|
||||
fin = open(fn, "r")
|
||||
theirlatestratchet = int(fin.readline())
|
||||
s = pow(theirlatestratchet, mylatestprivatekey, p)
|
||||
if messagenumber != 0:
|
||||
ciphertext = fin.readline().rstrip('\n')
|
||||
print("Received: " + otp(ciphertext, "dec", s))
|
||||
fin.close()
|
||||
os.remove(fn)
|
||||
messagenumber = messagenumber + 1
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user