Sunteți pe pagina 1din 9

Introduction to Security

17.10.2012

Patrick Rockenschaub

Assignment 1: Caesar Cipher


Caesar Cipher
The Caesar cipher is a very simple and easy to break cipher. Its basic principle is to simply exchange letters in the original text by shifting the alphabet left or right. The Cipher used in this example is not that easy but works with the same principle: a substitution cipher. In order to encrypt a plaintext a substitution alphabet is generated. In our case, the alphabet is mixed randomly, assigning each character of the original alphabet to a substitute character. Afterwards this new alphabet is applied to the plaintext, resulting in a weakly ciphered text. With some additional information, e.g. the language in which the plaintext was written or the topic of the text, we are quickly able to decipher the text. For this example I used additional knowledge of the average occurrences of the single letters in the English language (http://en.wikipedia.org/wiki/Letter_frequency) to pre-process the text. This way, characters are more likely in their correct place. Subsequently, I simply used the information, that Caesar wrote the text which lead to the first word (Caesar) immediately. Now that a few characters are already in the right spot, the remaining decryption of text was quite simple.

Original
eituih, rinsfy ltgivtl mwb livu sf mrim cgiet, kteixut rt ril ifmsescimtl mrim, sf mrt fimxhig ebxhut ba tntfmu, uxer wbxgl kt mrt ebflxem ba nthesfytmbhsq, gtintu mrt ihdv xflth chtmtfut ba hisusfy htehxsmu ifl einighv: rt cgietu khxmxu, i vbxfy dif, sf ebddifl ba mrtut abhetu; rt ysntu rsd sfumhxemsbfu mrim mrt einighv urbxgl hifyt iu tqmtfusntgv iu cbuuskgt sf igg lshtemsbfu; mrim rt wbxgl tqthm rsdutga fbm mb kt ikutfm ahbd mrt eidc gbfyth mrif mrhtt livu. rinsfy ihhifytl mrtut dimmthu, rt dihertu mb nstffi kv iu gbfy zbxhftvu iu rt eif, wrtf rsu bwf ubglsthu lsl fbm tqctem rsd. asflsfy mrtht i ahtur kblv ba einighv, wrser rt ril utfm bf mb mrim cgiet utnthig livu ktabht, dihersfy sfetuuifmgv fsyrm ifl liv, rt ilnifetl hicslgv mrhbxyr mrt mthhsmbhv ba mrt itlxs sfmb mrim ba mrt gsfybftu, sf wrser mwb gtysbfu wtht wsfmthsfy, mrim, sa ifv cgif iaatemsfy rsu bwf uiatmv urbxgl rint kttf bhyifsotl kv mrt itlxs, rt dsyrm ltatim sm kv mrt hicslsmv ba rsu dbntdtfmu. wrtf rt ihhsntl mrtht, rt utflu sfabhdimsbf mb mrt htum ba mrt gtysbfu, ifl yimrthu igg rsu ihdv sfmb bft cgiet ktabht sfmtggsytfet ba rsu ihhsnig ebxgl kt iffbxfetl mb mrt ihnthfs. nthesfytmbhsq, bf rtihsfy mrsu eshexdumifet, gtilu kiej rsu ihdv sfmb mrt ebxfmhv ba mrt ksmxhsytu; ifl iamth dihersfy ahbd sm mb ythybnsi, i mbwf ba mrt kbss, wrbd eituih ril utmmgtl mrtht iamth ltatimsfy mrtd sf mrt rtgntmsif wih, ifl ril htflthtl mhskxmihv mb mrt itlxs, rt ltmthdsftl mb immiej sm.

Histogram English language


[z: 7.4E-4%, q: 9.5E-4%, x: 0.0015%, j: 0.00153%, k: 0.00772%, v: 0.00978%, b: 0.01492%, p: 0.01929%, y: 0.01974%, g: 0.02015%, f: 0.02228%, w: 0.0236%, m: 0.02406%, u: 0.02758%, c: 0.02782%, l: 0.04025%, d: 0.04253%, r: 0.05987%, h: 0.06094%, s: 0.06327%, n: 0.06749%, i: 0.06966%, o: 0.07507%, a: 0.08167%, t: 0.09056%, e: 0.12702%]

Histogram Text

Introduction to Security

17.10.2012

Patrick Rockenschaub

[p: 0.0%, o: 6.849315068493151E-4%, z: 6.849315068493151E-4%, j: 0.0013698630136986301%, q: 0.003424657534246575%, c: 0.00821917808219178%, w: 0.010273972602739725%, k: 0.012328767123287671%, n: 0.014383561643835616%, d: 0.01643835616438356%, v: 0.01780821917808219%, x: 0.01780821917808219%, a: 0.01986301369863014%, y: 0.02054794520547945%, g: 0.026027397260273973%, e: 0.028767123287671233%, l: 0.03424657534246575%, u: 0.04315068493150685%, b: 0.04863013698630137%, h: 0.05136986301369863%, r: 0.05684931506849315%, s: 0.05821917808219178%, f: 0.06095890410958904%, i: 0.07260273972602739%, m: 0.07534246575342465%, t: 0.1047945205479452%]

Text after applying the histogram


caeras, nayiou delafed tbh dafr io tnat vlace, pecawre ne nad aoticivated tnat, io tne oatwsal chwsre hm eyeotr, rwcn bhwld pe tne chodwct hm yesciouethsik, leayer tne asgf wodes vseteore hm sairiou secswitr aod cayalsf: ne vlacer pswtwr, a fhwou gao, io chggaod hm tnere mhscer; ne uiyer nig iortswctihor tnat tne cayalsf rnhwld saoue ar ekteoriyelf ar vhrriple io all disectihor; tnat ne bhwld ekest nigrelm oht th pe apreot mshg tne cagv lhoues tnao tnsee dafr. nayiou assaoued tnere gattesr, ne gascner th yieooa pf ar lhou xhwsoefr ar ne cao, bneo nir hbo rhldiesr did oht ekvect nig. miodiou tnese a msern phdf hm cayalsf, bnicn ne nad reot ho th tnat vlace reyesal dafr pemhse, gascniou iocerraotlf oiunt aod daf, ne adyaoced savidlf tnshwun tne tessithsf hm tne aedwi ioth tnat hm tne liouhoer, io bnicn tbh leuihor bese biotesiou, tnat, im aof vlao ammectiou nir hbo rametf rnhwld naye peeo hsuaoiqed pf tne aedwi, ne giunt demeat it pf tne saviditf hm nir ghyegeotr. bneo ne assiyed tnese, ne reodr iomhsgatiho th tne sert hm tne leuihor, aod uatnesr all nir asgf ioth hoe vlace pemhse iotelliueoce hm nir assiyal chwld pe aoohwoced th tne asyesoi. yesciouethsik, ho neasiou tnir ciscwgrtaoce, leadr pacj nir asgf ioth tne chwotsf hm tne pitwsiuer; aod amtes gascniou mshg it th uesuhyia, a thbo hm tne phii, bnhg caeras nad rettled tnese amtes demeatiou tneg io tne nelyetiao bas, aod nad seodesed tsipwtasf th tne aedwi, ne detesgioed th attacj it.

Manual changes
The first word, laeras, looks very similar to caesar. As a result of the knowledge that the whole text is from ceasar I exchange l with c and s with r: l <--> c s <--> r
caesar, nayiou delafed tbh dafs io tnat vlace, pecawse ne nad aoticivated tnat, io tne oatwral chwrse hm eyeots, swcn bhwld pe tne chodwct hm yerciouethrik, leayes tne argf woder vreteose hm raisiou recrwits aod cayalrf: ne vlaces prwtws, a fhwou gao, io chggaod hm tnese mhrces; ne uiyes nig iostrwctihos tnat tne cayalrf snhwld raoue as ekteosiyelf as vhssiple io all directihos; tnat ne bhwld ekert nigselm oht th pe apseot mrhg tne cagv lhouer tnao tnree dafs. nayiou arraoued tnese gatters, ne garcnes th yieooa pf as lhou xhwroefs as ne cao, bneo nis hbo shldiers did oht ekvect nig. miodiou tnere a mresn phdf hm cayalrf, bnicn ne nad seot ho th tnat vlace seyeral dafs pemhre, garcniou iocessaotlf oiunt aod daf, ne adyaoced ravidlf tnrhwun tne territhrf hm tne aedwi ioth tnat hm tne liouhoes, io bnicn tbh leuihos bere bioteriou, tnat, im aof vlao ammectiou nis hbo sametf snhwld naye peeo hruaoiqed pf tne aedwi, ne giunt demeat it pf tne raviditf hm nis ghyegeots. bneo ne arriyed tnere, ne seods iomhrgatiho th tne rest hm tne leuihos, aod uatners all nis argf ioth hoe vlace pemhre iotelliueoce hm nis arriyal chwld pe aoohwoced th tne aryeroi. yerciouethrik, ho neariou tnis circwgstaoce, leads pacj nis argf ioth tne chwotrf hm tne pitwriues; aod amter garcniou mrhg it th ueruhyia, a thbo hm tne phii, bnhg caesar nad

Introduction to Security

17.10.2012

Patrick Rockenschaub

settled tnere amter demeatiou tneg io tne nelyetiao bar, aod nad reodered tripwtarf th tne aedwi, ne detergioed th attacj it.

Pecawse reminds me strongly of because, so the next thing I try is exchanging p with b and w with u: p <--> b w <--> u
caesar, nayiow delafed tph dafs io tnat vlace, because ne nad aoticivated tnat, io tne oatural churse hm eyeots, sucn phuld be tne choduct hm yerciowethrik, leayes tne argf uoder vreteose hm raisiow recruits aod cayalrf: ne vlaces brutus, a fhuow gao, io chggaod hm tnese mhrces; ne wiyes nig iostructihos tnat tne cayalrf snhuld raowe as ekteosiyelf as vhssible io all directihos; tnat ne phuld ekert nigselm oht th be abseot mrhg tne cagv lhower tnao tnree dafs. nayiow arraowed tnese gatters, ne garcnes th yieooa bf as lhow xhuroefs as ne cao, pneo nis hpo shldiers did oht ekvect nig. miodiow tnere a mresn bhdf hm cayalrf, pnicn ne nad seot ho th tnat vlace seyeral dafs bemhre, garcniow iocessaotlf oiwnt aod daf, ne adyaoced ravidlf tnrhuwn tne territhrf hm tne aedui ioth tnat hm tne liowhoes, io pnicn tph lewihos pere pioteriow, tnat, im aof vlao ammectiow nis hpo sametf snhuld naye beeo hrwaoiqed bf tne aedui, ne giwnt demeat it bf tne raviditf hm nis ghyegeots. pneo ne arriyed tnere, ne seods iomhrgatiho th tne rest hm tne lewihos, aod watners all nis argf ioth hoe vlace bemhre iotelliweoce hm nis arriyal chuld be aoohuoced th tne aryeroi. yerciowethrik, ho neariow tnis circugstaoce, leads bacj nis argf ioth tne chuotrf hm tne bituriwes; aod amter garcniow mrhg it th werwhyia, a thpo hm tne bhii, pnhg caesar nad settled tnere amter demeatiow tneg io tne nelyetiao par, aod nad reodered tributarf th tne aedui, ne detergioed th attacj it.

ne nad might be he had. Therefore I exchange n with h: n <--> h


caesar, hayiow delafed tpn dafs io that vlace, because he had aoticivated that, io the oatural cnurse nm eyeots, such pnuld be the cnoduct nm yerciowetnrik, leayes the argf uoder vreteose nm raisiow recruits aod cayalrf: he vlaces brutus, a fnuow gao, io cnggaod nm these mnrces; he wiyes hig iostructinos that the cayalrf shnuld raowe as ekteosiyelf as vnssible io all directinos; that he pnuld ekert higselm ont tn be abseot mrng the cagv lnower thao three dafs. hayiow arraowed these gatters, he garches tn yieooa bf as lnow xnuroefs as he cao, pheo his npo snldiers did ont ekvect hig. miodiow there a mresh bndf nm cayalrf, phich he had seot no tn that vlace seyeral dafs bemnre, garchiow iocessaotlf oiwht aod daf, he adyaoced ravidlf thrnuwh the territnrf nm the aedui iotn that nm the liownoes, io phich tpn lewinos pere pioteriow, that, im aof vlao ammectiow his npo sametf shnuld haye beeo nrwaoiqed bf the aedui, he giwht demeat it bf the raviditf nm his gnyegeots. pheo he arriyed there, he seods iomnrgatino tn the rest nm the lewinos, aod wathers all his argf iotn noe vlace bemnre iotelliweoce nm his arriyal cnuld be aoonuoced tn the aryeroi. yerciowetnrik, no heariow this circugstaoce, leads bacj his argf iotn the cnuotrf nm the bituriwes; aod amter garchiow mrng it tn werwnyia, a tnpo nm the bnii, phng caesar had settled there amter demeatiow theg io the helyetiao par, aod had reodered tributarf tn the aedui, he detergioed tn attacj it.

aoticivated looks like anticipated, so the next step is to exchange o with n and v with p: o <--> n v <--> p
caesar, hayinw delafed tvo dafs in that place, because he had anticipated that, in the natural course om eyents, such vould be the conduct om yercinwetorik, leayes the argf under pretense om raisinw recruits and cayalrf: he places brutus, a founw gan, in coggand om these morces; he wiyes hig instructions that the cayalrf should ranwe as ektensiyelf as possible in all directions; that he vould ekert higselm not to be absent mrog the cagp lonwer than three dafs. hayinw arranwed these gatters, he garches to yienna bf as lonw xournefs as he can, vhen his ovn soldiers did not ekpect hig. mindinw there a mresh bodf om cayalrf, vhich he had sent on to that place seyeral dafs bemore, garchinw incessantlf niwht and daf, he adyanced rapidlf throuwh the territorf om the aedui into that om the linwones, in vhich tvo lewions vere vinterinw, that, im anf plan ammectinw his ovn sametf should haye been orwaniqed bf the aedui, he giwht demeat it bf the rapiditf om his goyegents. vhen he arriyed there, he sends inmorgation to the rest om the lewions, and wathers all his argf into one place bemore intelliwence om his arriyal could be announced to the aryerni.

Introduction to Security

17.10.2012

Patrick Rockenschaub

yercinwetorik, on hearinw this circugstance, leads bacj his argf into the countrf om the bituriwes; and amter garchinw mrog it to werwoyia, a tovn om the boii, vhog caesar had settled there amter demeatinw theg in the helyetian var, and had rendered tributarf to the aedui, he detergined to attacj it.

hayinw reminds me of having, resulting in following changes: y <--> v w <--> g


caesar, having delafed tyo dafs in that place, because he had anticipated that, in the natural course om events, such yould be the conduct om vercingetorik, leaves the arwf under pretense om raising recruits and cavalrf: he places brutus, a foung wan, in cowwand om these morces; he gives hiw instructions that the cavalrf should range as ektensivelf as possible in all directions; that he yould ekert hiwselm not to be absent mrow the cawp longer than three dafs. having arranged these watters, he warches to vienna bf as long xournefs as he can, yhen his oyn soldiers did not ekpect hiw. minding there a mresh bodf om cavalrf, yhich he had sent on to that place several dafs bemore, warching incessantlf night and daf, he advanced rapidlf through the territorf om the aedui into that om the lingones, in yhich tyo legions yere yintering, that, im anf plan ammecting his oyn sametf should have been organiqed bf the aedui, he wight demeat it bf the rapiditf om his wovewents. yhen he arrived there, he sends inmorwation to the rest om the legions, and gathers all his arwf into one place bemore intelligence om his arrival could be announced to the arverni. vercingetorik, on hearing this circuwstance, leads bacj his arwf into the countrf om the bituriges; and amter warching mrow it to gergovia, a toyn om the boii, yhow caesar had settled there amter demeating thew in the helvetian yar, and had rendered tributarf to the aedui, he deterwined to attacj it.

delafed tyo dafs means very likely delayed two days: y <--> w f <--> y
caesar, having delayed two days in that place, because he had anticipated that, in the natural course om events, such would be the conduct om vercingetorik, leaves the arfy under pretense om raising recruits and cavalry: he places brutus, a young fan, in coffand om these morces; he gives hif instructions that the cavalry should range as ektensively as possible in all directions; that he would ekert hifselm not to be absent mrof the cafp longer than three days. having arranged these fatters, he farches to vienna by as long xourneys as he can, when his own soldiers did not ekpect hif. minding there a mresh body om cavalry, which he had sent on to that place several days bemore, farching incessantly night and day, he advanced rapidly through the territory om the aedui into that om the lingones, in which two legions were wintering, that, im any plan ammecting his own samety should have been organiqed by the aedui, he fight demeat it by the rapidity om his fovefents. when he arrived there, he sends inmorfation to the rest om the legions, and gathers all his arfy into one place bemore intelligence om his arrival could be announced to the arverni. vercingetorik, on hearing this circufstance, leads bacj his arfy into the country om the bituriges; and amter farching mrof it to gergovia, a town om the boii, whof caesar had settled there amter demeating thef in the helvetian war, and had rendered tributary to the aedui, he deterfined to attacj it.

om sounds like of and supported by the words hifselm and fatters, I exchanged f and m f <--> m
caesar, having delayed two days in that place, because he had anticipated that, in the natural course of events, such would be the conduct of vercingetorik, leaves the army under pretense of raising recruits and cavalry: he places brutus, a young man, in command of these forces; he gives him instructions that the cavalry should range as ektensively as possible in all directions; that he would ekert himself not to be absent from the camp longer than three days. having arranged these matters, he marches to vienna by as long xourneys as he can, when his own soldiers did not ekpect him. finding there a fresh body of cavalry, which he had sent on to that place several days before, marching incessantly night and day, he advanced rapidly through the territory of the aedui into that of the lingones, in which two legions were wintering, that, if any plan affecting his own safety should have been organiqed by the aedui, he might defeat it by the rapidity of his movements. when he arrived there, he sends information to the rest of the legions, and gathers all his army into one place before intelligence of his arrival could be announced to the arverni. vercingetorik, on hearing this circumstance, leads bacj his army into the country of the bituriges; and after marching from it to gergovia, a town of the boii, whom caesar had

Introduction to Security

17.10.2012

Patrick Rockenschaub

settled there after defeating them in the helvetian war, and had rendered tributary to the aedui, he determined to attacj it.

Asterix and Obelix has thaught children for ages that proper names end with x, so the implication that vercingetorik means vercingetorix is close to hand. xourneys on the other hand is probably journeys which leads to following exchanges: k <--> x k <--> j
caesar, having delayed two days in that place, because he had anticipated that, in the natural course of events, such would be the conduct of vercingetorix, leaves the army under pretense of raising recruits and cavalry: he places brutus, a young man, in command of these forces; he gives him instructions that the cavalry should range as extensively as possible in all directions; that he would exert himself not to be absent from the camp longer than three days. having arranged these matters, he marches to vienna by as long journeys as he can, when his own soldiers did not expect him. finding there a fresh body of cavalry, which he had sent on to that place several days before, marching incessantly night and day, he advanced rapidly through the territory of the aedui into that of the lingones, in which two legions were wintering, that, if any plan affecting his own safety should have been organiqed by the aedui, he might defeat it by the rapidity of his movements. when he arrived there, he sends information to the rest of the legions, and gathers all his army into one place before intelligence of his arrival could be announced to the arverni. vercingetorix, on hearing this circumstance, leads back his army into the country of the bituriges; and after marching from it to gergovia, a town of the boii, whom caesar had settled there after defeating them in the helvetian war, and had rendered tributary to the aedui, he determined to attack it.

The decryption is nearly done, only one remaining doesnt fit into the text properly: organiqed: q <--> z

Decrypted text
caesar, having delayed two days in that place, because he had anticipated that, in the natural course of events, such would be the conduct of vercingetorix, leaves the army under pretense of raising recruits and cavalry: he places brutus, a young man, in command of these forces; he gives him instructions that the cavalry should range as extensively as possible in all directions; that he would exert himself not to be absent from the camp longer than three days. having arranged these matters, he marches to vienna by as long journeys as he can, when his own soldiers did not expect him. finding there a fresh body of cavalry, which he had sent on to that place several days before, marching incessantly night and day, he advanced rapidly through the territory of the aedui into that of the lingones, in which two legions were wintering, that, if any plan affecting his own safety should have been organized by the aedui, he might defeat it by the rapidity of his movements. when he arrived there, he sends information to the rest of the legions, and gathers all his army into one place before intelligence of his arrival could be announced to the arverni. vercingetorix, on hearing this circumstance, leads back his army into the country of the bituriges; and after marching from it to gergovia, a town of the boii, whom caesar had settled there after defeating them in the helvetian war, and had rendered tributary to the aedui, he determined to attack it.

Key
a -> i b -> k c -> e

Introduction to Security d -> l e -> t f -> a g -> y h -> r i -> s j -> z k -> j l -> g m -> d n -> f o -> b p -> c q -> p r -> h s -> u t -> m u -> x v -> n w -> w x -> q y -> v z -> o

17.10.2012

Patrick Rockenschaub

Quick and Dirty Application Code


public class CeasarHelper { String cipherText = "eituih, rinsfy ltgivtl mwb livu sf mrim cgiet, kteixut rt ril ifmsescimtl mrim, sf mrt fimxhig ebxhut ba tntfmu, uxer wbxgl kt mrt ebflxem ba nthesfytmbhsq, gtintu mrt ihdv xflth chtmtfut ba hisusfy htehxsmu ifl einighv: rt cgietu khxmxu, i vbxfy dif, sf ebddifl ba mrtut abhetu; rt ysntu rsd sfumhxemsbfu mrim mrt einighv urbxgl hifyt iu tqmtfusntgv iu cbuuskgt sf igg lshtemsbfu; mrim rt wbxgl tqthm rsdutga fbm mb kt ikutfm ahbd mrt eidc gbfyth mrif mrhtt livu. rinsfy ihhifytl mrtut dimmthu, rt dihertu mb nstffi kv iu gbfy zbxhftvu iu rt eif, wrtf rsu bwf ubglsthu lsl fbm tqctem rsd. asflsfy mrtht i ahtur kblv ba einighv, wrser rt ril utfm bf mb mrim cgiet utnthig livu ktabht, dihersfy sfetuuifmgv fsyrm ifl liv, rt ilnifetl hicslgv mrhbxyr mrt mthhsmbhv ba mrt itlxs sfmb mrim ba mrt gsfybftu, sf wrser mwb gtysbfu wtht wsfmthsfy, mrim, sa ifv cgif iaatemsfy rsu bwf uiatmv urbxgl rint kttf bhyifsotl kv mrt itlxs, rt dsyrm ltatim sm kv mrt hicslsmv ba rsu dbntdtfmu. wrtf rt ihhsntl mrtht, rt utflu sfabhdimsbf mb mrt htum ba mrt gtysbfu, ifl yimrthu igg rsu ihdv sfmb bft cgiet ktabht sfmtggsytfet ba rsu ihhsnig ebxgl kt iffbxfetl mb mrt ihnthfs. nthesfytmbhsq, bf rtihsfy mrsu eshexdumifet, gtilu kiej rsu ihdv sfmb mrt ebxfmhv ba mrt ksmxhsytu; ifl iamth dihersfy ahbd sm mb ythybnsi, i mbwf ba mrt kbss, wrbd eituih ril utmmgtl mrtht iamth ltatimsfy mrtd sf mrt rtgntmsif wih, ifl ril htflthtl mhskxmihv mb mrt itlxs, rt ltmthdsftl mb immiej sm."; String deciphText = cipherText; ArrayList<Occurrence> enLangHist; ArrayList<Occurrence> cipherHist; public CeasarHelper(){ this.enLangHist = new ArrayList<Occurrence>(); this.cipherHist = new ArrayList<Occurrence>(); // pre-process data to simplify process this.initializeHistogramms(this.cipherText);

Introduction to Security
this.applyHistogramm();

17.10.2012

Patrick Rockenschaub

System.out.println(this.enLangHist); System.out.println(this.cipherHist); Scanner in = new Scanner(System.in); String cmd1 = "", cmd2 = ""; String history = ""; // Exchange two characters until the cipher is decoded while(!cmd1.equals("exit")){ System.out.println(this.deciphText); System.out.print("Character A: "); cmd1 = in.nextLine(); if(cmd1.equals("exit")){ break; } System.out.print("Character B: "); cmd2 = in.nextLine(); exchangeCharacter(cmd1.charAt(0), cmd2.charAt(0)); history += cmd1.charAt(0) + " <--> " + cmd2.charAt(0) + "\n"; } // print out history of changes System.out.println(); System.out.println(history); } /* * Simply exchange two character in the cipher text */ private void exchangeCharacter(char a, char b){ String newDeciphe = ""; char c; for(int i = 0; i < this.deciphText.length(); i++){ c = this.deciphText.charAt(i); if(c == a){ newDeciphe += b; }else if(c == b){ newDeciphe += a; }else{ newDeciphe += c; } } this.deciphText = newDeciphe; } /* * Exchange letters initially according to their sorted order * in the histogram. This way, letters will be in the right place * more likely, simplifying the process of manual decoding. */ private void applyHistogramm(){ char c; int p; String newDeciphe = ""; for(int i = 0; i < this.deciphText.length(); i++){ c = this.deciphText.charAt(i);

Introduction to Security

17.10.2012

Patrick Rockenschaub

p = this.cipherHist.indexOf(new Occurrence(c, 0.0)); if(p == -1){ newDeciphe += c; }else{ newDeciphe += this.enLangHist.get(p).character; } } this.deciphText = newDeciphe; } /* * Count the character occurrences in the cipher text and * build two histograms, one representing the average usage * of the alphabet in the English language and the other * the specific occurrences in this text. The histograms * are sorted according to the relative occurrences. (Data * for English language: http://en.wikipedia.org/wiki/Letter_frequency) */ private void initializeHistogramms(String cipher){ this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new this.enLangHist.add(new Occurrence('a', Occurrence('b', Occurrence('c', Occurrence('d', Occurrence('e', Occurrence('f', Occurrence('g', Occurrence('h', Occurrence('i', Occurrence('j', Occurrence('k', Occurrence('l', Occurrence('m', Occurrence('n', Occurrence('o', Occurrence('p', Occurrence('q', Occurrence('r', Occurrence('s', Occurrence('t', Occurrence('u', Occurrence('v', Occurrence('w', Occurrence('x', Occurrence('y', Occurrence('z', 0.08167)); 0.01492)); 0.02782)); 0.04253)); 0.12702)); 0.02228)); 0.02015)); 0.06094)); 0.06966)); 0.00153)); 0.00772)); 0.04025)); 0.02406)); 0.06749)); 0.07507)); 0.01929)); 0.00095)); 0.05987)); 0.06327)); 0.09056)); 0.02758)); 0.00978)); 0.02360)); 0.00150)); 0.01974)); 0.00074));

// Cipher-Hist int total = cipher.length(); for(char c = 'a'; c <='z'; c++){ int count = 0; for (int i=0; i < cipher.length(); i++){ if (cipher.charAt(i) == c){ count++; } } this.cipherHist.add(new Occurrence(c, (double) count/total)); }

Introduction to Security

17.10.2012

Patrick Rockenschaub

// Sort the characters to be able to exchange them before trying // to solve the cipher manually Collections.sort(this.enLangHist); Collections.sort(this.cipherHist); } /* * Represents a character and its relative frequency */ class Occurrence implements Comparable<Occurrence>{ public Character character; public Double percentage; public Occurrence(char character, double percentage){ this.character = character; this.percentage = percentage; } public int compareTo(Occurrence other){ return this.percentage.compareTo(other.percentage); } public String toString(){ return this.character + ": " + this.percentage.toString() + "%"; } @Override public boolean equals(Object other){ Occurrence o = (Occurrence)other; return this.character == o.character; } } }

S-ar putea să vă placă și