Files
lab_encryption/caesar_cracker.py
2026-04-11 10:17:48 -04:00

193 lines
13 KiB
Python

# Nelson Mason -- LAI677LEC LOA -- 4-9-2026
from easybits import Bits
from collections import Counter
from ciphers.caesar import CaesarCipher
# secret0.txt
# ciphertext="ChVTanChVTaznQda]X]VnQaXVWczn8]ncWTnU^aTbcbn^UncWTn]XVWc*nFWPcnX\\^acP[nWP]Sn^anThTzn2^d[SnUaP\\TncWhnUTPaUd[nbh\\Tcah."
# ciphertext="8]nfWPcnSXbcP]cnSTT_bn^anbZXTb|n1da]cncWTnUXaTn^UncWX]TnThTb.>]nfWPcnfX]VbnSPaTnWTnPb_XaT.FWPcncWTnWP]SznSPaTnbTXiTncWTnUXaT."
# ciphertext="0]SnfWPcnbW^d[STazntnfWPcnPacz2^d[SncfXbcncWTnbX]Tfbn^UncWhnWTPac.0]SnfWT]ncWhnWTPacnQTVP]nc^nQTPc|FWPcnSaTPSnWP]S.ntnfWPcnSaTPSnUTTc."
# ciphertext="FWPcncWTnWP\\Ta.nfWPcncWTnRWPX]z8]nfWPcnUda]PRTnfPbncWhnQaPX].FWPcncWTnP]eX[.nfWPcnSaTPSnVaPb_|3PaTnXcbnSTPS[hncTaa^abnR[Pb_."
# ciphertext="FWT]ncWTnbcPabncWaTfnS^f]ncWTXanb_TPabn0]SnfPcTauSnWTPeT]nfXcWncWTXancTPab)3XSnWTnb\\X[TnWXbnf^aZnc^nbTT.3XSnWTnfW^n\\PSTncWTn;P\\Qn\\PZTncWTT."
# ciphertext="ChVTanChVTanQda]X]VnQaXVWcz8]ncWTnU^aTbcbn^UncWTn]XVWc)FWPcnX\\^acP[nWP]Sn^anThTz3PaTnUaP\\TncWhnUTPaUd[nbh\\Tcah."
# secret1.txt
# ciphertext="Pof!nvtu!ibwf!b!njoe!pg!xjoufsUp!sfhbse!uif!gsptu!boe!uif!cpvhitPg!uif!qjof.usfft!dsvtufe!xjui!topx<"
# ciphertext="Boe!ibwf!cffo!dpme!b!mpoh!ujnfUp!cfipme!uif!kvojqfst!tibhhfe!xjui!jdf-Uif!tqsvdft!spvhi!jo!uif!ejtubou!hmjuufs"
# ciphertext="Pg!uif!Kbovbsz!tvo<!boe!opu!up!uijolPg!boz!njtfsz!jo!uif!tpvoe!pg!uif!xjoe-Jo!uif!tpvoe!pg!b!gfx!mfbwft-"
# ciphertext="Xijdi!jt!uif!tpvoe!pg!uif!mboeGvmm!pg!uif!tbnf!xjoeUibu!jt!cmpxjoh!jo!uif!tbnf!cbsf!qmbdf"
# ciphertext="Gps!uif!mjtufofs-!xip!mjtufot!jo!uif!topx-Boe-!opuijoh!ijntfmg-!cfipmetOpuijoh!uibu!jt!opu!uifsf!boe!uif!opuijoh!uibu!jt/"
# secret2.txt
# ciphertext="z<DL.@LuLD.@LF<B;4L.;1L2.@FLB;12?LA52L.==92L/<B45@m/<BALA52L969A6;4L5<B@2L.;1L5.==FL.@LA52L4?.@@LD.@L4?22;XLLLLL!52L;645AL./<C2LA52L16;492L@A.??FX"
# ciphertext="LLLLLLLLLL!6:2L92AL:2L5.69L.;1L096:/LLLLLs<912;L6;LA52L52F1.F@L<3L56@L2F2@Xm;1L5<;<B?21L.:<;4LD.4<;@LuLD.@L=?6;02L<3LA52L.==92LA<D;@"
# ciphertext="m;1L<;02L/29<DL.LA6:2LuL9<?19FL5.1LA52LA?22@L.;1L92.C2@LLLLLLLLLL!?.69LD6A5L1.6@62@L.;1L/.?92FLLLLLp<D;LA52L?6C2?@L<3LA52LD6;13.99L9645AZ"
# ciphertext="m;1L.@LuLD.@L4?22;L.;1L0.?23?22XL3.:<B@L.:<;4LA52L/.?;@m/<BALA52L5.==FLF.?1L.;1L@6;46;4L.@LA52L3.?:LD.@L5<:2XLLLLLu;LA52L@B;LA5.AL6@LF<B;4L<;02L<;9FX"
# ciphertext="LLLLLLLLLL!6:2L92AL:2L=9.FL.;1L/2LLLLLs<912;L6;LA52L:2?0FL<3L56@L:2.;@Xm;1L4?22;L.;1L4<912;LuLD.@L5B;A@:.;L.;1L52?1@:.;XLA52L0.9C2@"
# ciphertext=" .;4LA<L:FL5<?;XLA52L3<E2@L<;LA52L5699@L/.?821L092.?L.;1L0<91XLLLLLLLLLLm;1LA52L@.//.A5L?.;4L@9<D9FLLLLLu;LA52L=2//92@L<3LA52L5<9FL@A?2.:@Z"
# ciphertext="m99LA52L@B;L9<;4L6ALD.@L?B;;6;4XL6ALD.@L9<C29FXLA52L5.Fr6291@L5645L.@LA52L5<B@2XLA52LAB;2@L3?<:LA52L056:;2F@XL6ALD.@L.6?LLLLLm;1L=9.F6;4XL9<C29FL.;1LD.A2?F"
# ciphertext="LLLLLLLLLLm;1L36?2L4?22;L.@L4?.@@ZLLLLLm;1L;645A9FLB;12?LA52L@6:=92L@A.?@m@LuL?<12LA<L@922=LA52L<D9@LD2?2L/2.?6;4LA52L3.?:L.D.FX"
# ciphertext="m99LA52L:<<;L9<;4LuL52.?1XL/92@@21L.:<;4L@A./92@XLA52L;645A7.?@LLLLLr9F6;4LD6A5LA52L?608@XL.;1LA52L5<?@2@LLLLLLLLLLr9.@56;4L6;A<LA52L1.?8Z"
# ciphertext="m;1LA52;LA<L.D.82XL.;1LA52L3.?:XL9682L.LD.;12?2?LD56A2$6A5LA52L12DXL0<:2L/.08XLA52L0<08L<;L56@L@5<B912?fL6ALD.@L.99LLLLL 56;6;4XL6ALD.@Lm1.:L.;1L:.612;X"
# ciphertext="LLLLLLLLLL!52L@8FL4.A52?21L.4.6;LLLLLm;1LA52L@B;L4?2DL?<B;1LA5.ALC2?FL1.FZ<L6AL:B@AL5.C2L/22;L.3A2?LA52L/6?A5L<3LA52L@6:=92L9645A"
# ciphertext="u;LA52L36?@AXL@=6;;6;4L=9.02XLA52L@=299/<B;1L5<?@2@LD.986;4LD.?:LLLLL{BAL<3LA52LD56;;F6;4L4?22;L@A./92LLLLLLLLLL{;LA<LA52L36291@L<3L=?.6@2Z"
# ciphertext="m;1L5<;<B?21L.:<;4L3<E2@L.;1L=52.@.;A@L/FLA52L4.FL5<B@2;12?LA52L;2DL:.12L09<B1@L.;1L5.==FL.@LA52L52.?ALD.@L9<;4XLLLLLu;LA52L@B;L/<?;L<C2?L.;1L<C2?X"
# ciphertext="LLLLLLLLLLuL?.;L:FL522192@@LD.F@XLLLLLyFLD6@52@L?.021LA5?<B45LA52L5<B@2L5645L5.Fm;1L;<A56;4LuL0.?21XL.AL:FL@8FL/9B2LA?.12@XLA5.ALA6:2L.99<D@"
# ciphertext="u;L.99L56@LAB;23B9LAB?;6;4L@<L32DL.;1L@B05L:<?;6;4L@<;4@LLLLLn23<?2LA52L05691?2;L4?22;L.;1L4<912;LLLLLLLLLLr<99<DL56:L<BAL<3L4?.02X"
# ciphertext="z<A56;4LuL0.?21XL6;LA52L9.:/LD56A2L1.F@XLA5.ALA6:2LD<B91LA.82L:2=LA<LA52L@D.99<DLA5?<;421L9<3AL/FLA52L@5.1<DL<3L:FL5.;1XLLLLLu;LA52L:<<;LA5.AL6@L.9D.F@L?6@6;4X"
# ciphertext="LLLLLLLLLLz<?LA5.AL?616;4LA<L@922=LLLLLuL@5<B91L52.?L56:L39FLD6A5LA52L5645L36291@m;1LD.82LA<LA52L3.?:L3<?2C2?L3921L3?<:LA52L0569192@@L9.;1Z"
# ciphertext="{5L.@LuLD.@LF<B;4L.;1L2.@FL6;LA52L:2?0FL<3L56@L:2.;@XLLLLLLLLLL!6:2L5291L:2L4?22;L.;1L1F6;4LLLLL!5<B45LuL@.;4L6;L:FL05.6;@L9682LA52L@2.Z"
# secret3.txt
# ciphertext="3DAJ[PK[PDA[OAOOEKJO[KB[OSAAP[OEHAJP[PDKQCDP%[OQIIKJ[QL[NAIAI>N=J?A[KB[PDEJCO[L=OPg%[OECD[PDA[H=?G[KB[I=JU[=[PDEJC[%[OKQCDPg"
# ciphertext="|J@[SEPD[KH@[SKAO[JAS[S=EH[IU[@A=N[PEIAbO[S=OPAu0DAJ[?=J[%[@NKSJ[=J[AUAg[QJQOb@[PK[BHKSgKN[LNA?EKQO[BNEAJ@O[DE@[EJ[@A=PDbO[@=PAHAOO[JECDPg"
# ciphertext="|J@[SAAL[=BNAOD[HKRAbO[HKJC[OEJ?A[?=J?AHHb@[SKAg|J@[IK=J[PDb[ATLAJOA[KB[I=JU[=[R=JEODb@[OECDPv0DAJ[?=J[%[CNEARA[=P[CNEAR=J?AO[BKNACKJAg"
# ciphertext="|J@[DA=REHU[BNKI[SKA[PK[SKA[PAHH[KbAN0DA[O=@[=??KQJP[KB[BKNAh>AIK=JA@[IK=Jg3DE?D[%[JAS[L=U[=O[EB[JKP[L=E@[>ABKNAi}QP[EB[PDA[SDEHA[%[PDEJG[KJ[PDAAg[@A=N[BNEAJ@g|HH[HKOOAO[=NA[NAOPKNb@g[=J@[OKNNKSO[AJ@i"
# secret4.txt
# ciphertext="""l"5&@a6(645L@(*7&/@)&"7:@3"*/@"/%@46/f03@"@'6--@8&&,L@5)&@#-"$,#&33*&4@806-%@3*1&/Na5@'*345L@+645@0/&L@"@(-044:@1631-&@$-05a.0/(@05)&34L@3&%L@(3&&/L@)"3%@"4@"@,/05N"""
# use triple quotes before and after the string
# ciphertext="""y06@"5&@5)"5@'*345@0/&@"/%@*54@'-&4)@8"4@48&&5l*,&@5)*$,&/&%@8*/&Z@46..&3G4@#-00%@8"4@*/@*5l&"7*/(@45"*/4@610/@5)&@50/(6&@"/%@-645@'03p*$,*/(N@t)&/@3&%@0/&4@*/,&%@61@"/%@5)"5@)6/(&3"""
# ciphertext="""s&/5@64@065@8*5)@.*-,@$"/4L@1&"@5*/4L@+".M1054w)&3&@#3*"34@4$3"5$)&%@"/%@8&5@(3"44@#-&"$)&%@063@#0054Nr06/%@)":'*&-%4L@$03/'*&-%4@"/%@105"50M%3*--4w&@53&,,&%@"/%@1*$,&%@6/5*-@5)&@$"/4@8&3&@'6--L"""
# ciphertext="""u/5*-@5)&@5*/,-*/(@#0550.@)"%@#&&/@$07&3&%w*5)@(3&&/@0/&4L@"/%@0/@501@#*(@%"3,@#-0#4@#63/&%l*,&@"@1-"5&@0'@&:&4N@o63@)"/%4@8&3&@1&11&3&%w*5)@5)03/@13*$,4L@063@1"-.4@45*$,:@"4@b-6&#&"3%G4N"""
# ciphertext="""w&@)0"3%&%@5)&@'3&4)@#&33*&4@*/@5)&@#:3&Nb65@8)&/@5)&@#"5)@8"4@'*--&%@8&@'06/%@"@'63La@3"5M(3&:@'6/(64L@(-655*/(@0/@063@$"$)&Nt)&@+6*$&@8"4@45*/,*/(@500N@o/$&@0''@5)&@#64)"""
# ciphertext="""t)&@'36*5@'&3.&/5&%L@5)&@48&&5@'-&4)@806-%@563/@4063Ni@"-8":4@'&-5@-*,&@$3:*/(N@i5@8"4/G5@'"*3t)"5@"--@5)&@-07&-:@$"/'6-4@4.&-5@0'@305Ne"$)@:&"3@i@)01&%@5)&:G%@,&&1L@,/&8@5)&:@806-%@/05N"""
# checkpoint 2 - polyalphabetic encryption/decryption
# ciphertext=":y]JfTp\j[YTUyiQUnd_X[UcpngNQbflZhY]pgnhRPTenJbS|yjWTTcyiQUn`[`hdaV_#"
# length of keyword is probably 6
# 6 columns - now use frequency analysis and monoalphabetic encryption/decryption
# ciphertext=":pUdpfpT|c`V"
# ciphertext="y\y_nlgeyy[_"
# ciphertext="]jiXgZnnji`#"
# ciphertext="J[Q[NhhJWQh"
# ciphertext="fYUUQYRbTUd"
ciphertext="TTncb]PSTna"
data=Counter(ciphertext)
keys_list = list(data.keys()) # unpack into a key list
values_list = list(data.values()) # and a values list
v=max(values_list) # get the largest value - most frequent - frequency analysis
values_list.index(v) # get the index of the largest value
idx=values_list.index(v) # put that index into a variable
key_value=keys_list[idx] # apply that same variable to the keys list
# print(keys_list)
# print(values_list)
# print(v)
# #print(values_list.index(v)) - prints the index number
# print(key_value)
print(data)
secret=Bits(str(key_value)).int - Bits('e').int # monoalphabetic offset number
print(secret) # secret number
cipher = CaesarCipher(secret) # I get a runtime warning message referencing
# the variable "ciphertext" - invalid escape sequence.
cipher.decrypt(ciphertext)
print(cipher.decrypt(ciphertext))
# secret0
# 'Tyger Tyger, burning bright, In the forests of the night; What imortal hand or eye, Could frame thy fearful symetry?'
# In what distant deeps or skies. Burnt the fire of thine eyes?On what wings dare he aspire?What the hand, dare seize the fire?
# And what shoulder, & what art,Could twist the sinews of thy heart?And when thy heart began to beat.What dread hand? & what dread feet?
# What the hamer? what the chain,In what furnace was thy brain?What the anvil? what dread grasp.Dare its deadly terrors clasp?
# When the stars threw down their spears And water'd heaven with their tears:Did he smile his work to see?Did he who made the Lamb make thee?
# Tyger Tyger burning bright,In the forests of the night:What imortal hand or eye,Dare frame thy fearful symetry?
# secret1
# One must have a mind of winter To regard the frost and the boughs Of the pine-trees crusted with snow;
# And have been cold a long time To behold the junipers shagged with ice,The spruces rough in the distant glitter
# Of the January sun; and not to think Of any misery in the sound of the wind,In the sound of a few leaves,
# Which is the sound of the land Full of the same wind That is blowing in the same bare place
# For the listener, who listens in the snow,And, nothing himself, beholdsNothing that is not there and the nothing that is.
# secret2
# Now as I was young and easy under the apple boughsAbout the lilting house and happy as the grass was green,The night above the dingle starry,
# Time let me hail and climb Golden in the heydays of his eyes,And honoured among wagons I was prince of the apple towns
# And once below a time I lordly had the trees and leaves Trail with daisies and barley Down the rivers of the windfall light.
# And as I was green and carefree, famous among the barns About the happy yard and singing as the farm was home,In the sun that is young once only,
# Time let me play and be Golden in the mercy of his means,And green and golden I was huntsman and herdsman, the calves
# Sang to my horn, the foxes on the hills barked clear and cold,And the sabbath rang slowly In the pebbles of the holy streams.
# All the sun long it was running, it was lovely, the hay Fields high as the house, the tunes from the chimneys, it was air And playing, lovely and watery
# And fire green as grass. And nightly under the simple stars As I rode to sleep the owls were bearing the farm away,
# All the moon long I heard, blessed among stables, the nightjars Flying with the ricks, and the horses Flashing into the dark.
# And then to awake, and the farm, like a wanderer white With the dew, come back, the cock on his shoulder: it was all Shining, it was Adam and maiden,
# The sky gathered again And the sun grew round that very day.o it must have been after the birth of the simple light
# In the first, spinning place, the spellbound horses walking warm Out of the whinnying green stable On to the fields of praise.
# And honoured among foxes and pheasants by the gay housender the new made clouds and happy as the heart was long,In the sun born over and over,
# I ran my heedless ways,My wishes raced through the house high hay And nothing I cared, at my sky blue trades, that time allows
# In all his tuneful turning so few and such morning songs Before the children green and golden Follow him out of grace,
# Nothing I cared, in the lamb white days, that time would take mep to the swallow thronged loft by the shadow of my hand,In the moon that is always rising,
# Nor that riding to sleep I should hear him fly with the high fields And wake to the farm forever fled from the childless land.
# Oh as I was young and easy in the mercy of his means,Time held me green and dying Though I sang in my chains like the sea.
# secret3
# When to the sessions of sweet silent thought I summon up remembrance of things past,I sigh the lack of many a thing I sought,
# And with old woes new wail my dear time's waste:Then can I drown an eye, unus'd to flow,or precious friends hid in death's dateless night,
# And weep afresh love's long since cancell'd woe,And moan th' expense of many a vanish'd sight;Then can I grieve at grievances foregone,
# And heavily from woe to woe tell o'er The sad account of fore-bemoaned moan,Which I new pay as if not paid before.
# But if the while I think on thee, dear friend,All losses are restor'd, and sorrows end.
# secret4
# Late August, given heavy rain and sun For a full week, the blackberries would ripen.
# At first, just one, a glossy purple clot Among others, red, green, hard as a knot.
# You ate that first one and its flesh was sweetLike thickened wine: summer's blood was in it
# Leaving stains upon the tongue and lust for Picking. Then red ones inked up and that hunger
# Sent us out with milk cans, pea tins, jam-pots Where briars scratched and wet grass bleached our boots.
# Round hayfields, cornfields and potato-drills We trekked and picked until the cans were full,
# Until the tinkling bottom had been covered With green ones, and on top big dark blobs burned Like a plate of eyes.
# Our hands were peppered With thorn pricks, our palms sticky as Bluebeard's.
# We hoarded the fresh berries in the byre.But when the bath was filled we found a fur,A rat-grey fungus, glutting on our cache.
# The juice was stinking too. Once off the bush The fruit fermented, the sweet flesh would turn sour.
# I always felt like crying. It wasn't fair That all the lovely canfuls smelt of rot.
# Each year I hoped they'd keep, knew they would not.