SharedHandouts / Vigenere Example / Vigenere example.sagewsOpen in CoCalc
Author: Nathan McNew
Views : 8
Description: Breaking the Vigenere Cipher
ciphertext="EVUYIKDGGMYSEEWWYYSPSKYULEEDQZGICESLIKEXAWESTEMGYKWCMINHGRVYSLWFSVZRYJWUWVRMGUGCJMHFKAJVRAWKXCPIWMHENCRBYZXYRWLWSXGUEMLZIDIITCXEUKXTTDIKJXHGVYRFRMZWLGRVMZJYIRRWHAGMVHNHLLGJXBUCAJZGAGMVXZWBLGVJRHEWXXDVLBFVLKDXAWFMIYXUWXSTVMMOSWCWMGWLLKTOIAWGGFJPGGHDLPUGMWJVEWWVSHKLXKGVVNLBUZMUBRHOFEURTXFSRICEPQWVLLWMMFHGIXAWZICUATKSFTFEWKAPXVVUSFHPVEKDQEPZRVZSGTFWLLGNCDILEGVVZQXJEVEJJKGELKJJKAWRFJSYLZIETLPSKIPXVTNWHWGSGALAKKLMZWHCKIBLOEUAYLLKYEYELLAGMRWMZWSNUJTKZMQEIWXSQKCCIJSGVZXBGFITLWXVLSERVKQVMIEMYAWHUFPBVSRFIITKKYTZRZOWPNNEMKGRYYEMVGCQLQTCWSHZXAGDQGJATKKMVKMGYOMVYLBKTEEBXHEWEPUMASVKKMIGZAQPFWBYFSHDCHUUYRRXBGFLQNHBVQSWBRHOOLCKMPSKHQZRZATINZIOWQSWYEOWWCGJMGLZIDRGDGXCQLVAWSHKYEOWSXNVELLSAGCPIGDMUYIWKAPXVVIDSXGUGHXXIGGSMAFJTFRMGXQGJEBVZIDLXMWDPOVATLKSPNLTLVSAFYFSCIQWSNJNMUZXHJKWVZGDKAREVAXZSZGSIXFKSWEJHJLYPRXXSKXQDMLKZMORRWZSZGESGGLMQESYZAWGIVTFVXJZWTUUMFVRMSDWQLZXFAVDVGHEWWQWMFHGVVRRVWDIVDIAWSVAFYKWUSPJXKMUXVYIFSFFARRXPSQKEEMAGRQWMMALLKEOLSAHKWSEDGAKEKLGXETRWBUGYNUXAWEIVYSWKGJOPG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XVVMWFXNPNNKLENZXMDWWCZHAGDQGJMMZARMUVFGJXKDIKQGYYFYEVVSYZWXDQMHNMMZGYVDSKWSHQPSNOGYNUOBFVPAKIEDEIRCEBFDCYYEMLZIGOEVLFEVLVXGXXJVTKGTPGDMLAFAJZGAQGYFVQTFVQARWLAKXCEGX"
def stringShift(str,n): slen=len(str) return str[n:slen+1]+str[0:n] def countCoincidences(str1,str2): slen=len(str1) coin=0 for j in [0..slen-1]: if str1[j]==str2[j]: coin=coin+1 return coin def breakStringMod(str,m): cts=['']*m slen=len(str) for i in range(slen): cts[i%m]=cts[i%m]+str[i] return cts def countLetterFrequencies(str): alph='ABCDEFGHIJKLMNOPQRSTUVWXYZ' for i in alph: print i, str.count(i)
stringShift("EXAMPLE",-1)
'EEXAMPL'
breakStringMod("HIMYNAMEISDRMCNEW",3)
['HYMSME', 'INEDCW', 'MAIRN']
countLetterFrequencies("AAAXYCYC")
A 3 B 0 C 2 D 0 E 0 F 0 G 0 H 0 I 0 J 0 K 0 L 0 M 0 N 0 O 0 P 0 Q 0 R 0 S 0 T 0 U 0 V 0 W 0 X 1 Y 2 Z 0
countCoincidences("HIM","HAY")
1
for i in [1..30]: shiftedCT=stringShift(ciphertext,i) numCoincidences = countCoincidences(ciphertext,shiftedCT) print i,numCoincidences
1 457 2 345 3 403 4 382 5 340 6 418 7 625 8 435 9 397 10 393 11 429 12 356 13 426 14 608 15 404 16 377 17 388 18 392 19 313 20 419 21 636 22 388 23 362 24 366 25 386 26 383 27 416 28 621 29 385 30 355
#Guess that our key length is 7
every7thLetter=breakStringMod(ciphertext,7)
every7thLetter[2]
'UMYUGKTCVVWCJCNYGDUKRGIMGJXJDKITCKGDJHVUUILGCTXPPTCVEKFEPWKCUEMNQKVTEIUFTNYQHGVVEPKPHRQWCQNWGDQKNGUXGGTGDOPAQUVEGWPQOGQGJFQDQVVAPVAKQKKKTNVOPVKUHTEGGEYJJOJTQCGNKVRNPQKPEGGVJVQQGKIPGUJUGVPKCCPAJ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GAQGPCFUCGUOKVVKEGKVGQQCTKGVCTVQCOVJGQEGIICPJRGQPQVKANCEVOPVPCUAPJGPQAOKKAJFVKGUKYJTGUOUGVHGPKQPGPYGPPPGNCQEEKWCOHKYVUQUCPXNNCGMKYYHVQNARYGVJGJFAC'
for i in [0..6]: print "Letter frequencies in position ", i+1 countLetterFrequencies(every7thLetter[i])
Letter frequencies in position 1 A 93 B 2 C 9 D 68 E 47 F 97 G 130 H 23 I 1 J 79 K 106 L 116 M 35 N 18 O 27 P 2 Q 39 R 0 S 112 T 19 U 30 V 51 W 142 X 38 Y 20 Z 77 Letter frequencies in position 2 A 34 B 1 C 25 D 1 E 115 F 24 G 34 H 56 I 148 J 32 K 28 L 63 M 114 N 3 O 10 P 65 Q 42 R 92 S 121 T 28 U 1 V 73 W 93 X 107 Y 48 Z 23 Letter frequencies in position 3 A 34 B 0 C 97 D 19 E 55 F 42 G 155 H 31 I 23 J 78 K 108 L 2 M 20 N 55 O 42 P 97 Q 113 R 27 S 0 T 83 U 96 V 117 W 39 X 14 Y 33 Z 1 Letter frequencies in position 4 A 2 B 16 C 45 D 49 E 91 F 96 G 24 H 0 I 99 J 87 K 104 L 45 M 12 N 26 O 3 P 36 Q 0 R 107 S 14 T 39 U 49 V 153 W 31 X 23 Y 110 Z 120 Letter frequencies in position 5 A 28 B 2 C 35 D 0 E 138 F 9 G 42 H 45 I 157 J 27 K 22 L 73 M 122 N 4 O 16 P 44 Q 33 R 94 S 119 T 18 U 2 V 74 W 94 X 134 Y 36 Z 13 Letter frequencies in position 6 A 96 B 101 C 2 D 10 E 41 F 44 G 86 H 107 I 19 J 1 K 83 L 104 M 132 N 46 O 15 P 28 Q 6 R 22 S 0 T 106 U 21 V 44 W 62 X 147 Y 29 Z 29 Letter frequencies in position 7 A 121 B 0 C 10 D 60 E 39 F 100 G 123 H 19 I 0 J 74 K 87 L 119 M 35 N 8 O 30 P 2 Q 27 R 0 S 135 T 26 U 47 V 47 W 140 X 24 Y 29 Z 78
for i in [1..30]: shiftedString=stringShift(ciphertext,i) print i,countCoincidences(ciphertext,shiftedString)
1 457 2 345 3 403 4 382 5 340 6 418 7 625 8 435 9 397 10 393 11 429 12 356 13 426 14 608 15 404 16 377 17 388 18 392 19 313 20 419 21 636 22 388 23 362 24 366 25 386 26 383 27 416 28 621 29 385 30 355
#Sage code to decipher text: key="SECRETS" alphabet = AlphabeticStrings() ciphertextEncoded = alphabet.encoding(ciphertext) system = VigenereCryptosystem(alphabet,len(key)) key = alphabet(key)
plaintext = system.deciphering(key, ciphertextEncoded) plaintext
MRSHERLOCKHOLMESWHOWASUSUALLYVERYLATEINTHEMORNINGSSAVEUPONTHOSENOTINFREQUENTOCCASIONSWHENHESTAYEDUPALLNIGHTWASSEATEDATTHEBREAKFASTTABLEISTOODUPONTHEHEARTHRUGANDPICKEDUPTHESTICKWHICHOURVISITORHADLEFTBEHINDHIMTHENIGHTBEFOREITWASAFINETHICKPIECEOFWOODBULBOUSHEADEDOFTHESORTWHICHISKNOWNASAPENANGLAWYERJUSTUNDERTHEHEADWASABROADSILVERBANDNEARLYANINCHACROSSTOJAMESMORTIMERMRCSFROMHISFRIENDSOFTHECCHWASENGRAVEDUPONITWITHTHEDATEITWASJUSTSUCHASTICKASTHEOLDFASHIONEDFAMILYPRACTITIONERUSEDTOCARRYDIGNIFIEDSOLIDANDREASSURINGWELLWATSONWHATDOYOUMAKEOFITHOLMESWASSITTINGWITHHISBACKTOMEANDIHADGIVENHIMNOSIGNOFMYOCCUPATIONHOWDIDYOUKNOWWHATIWASDOINGIBELIEVEYOUHAVEEYESINTHEBACKOFYOURHEADIHAVEATLEASTAWELLPOLISHEDSILVERPLATEDCOFFEEPOTINFRONTOFMESAIDHEBUTTELLMEWATSONWHATDOYOUMAKEOFOURVISITORSSTICKSINCEWEHAVEBEENSOUNFORTUNATEASTOMISSHIMANDHAVENONOTIONOFHISERRANDTHISACCIDENTALSOUVENIRBECOMESOFIMPORTANCELETMEHEARYOURECONSTRUCTTHEMANBYANEXAMINATIONOFITITHINKSAIDIFOLLOWINGSOFARASICOULDTHEMETHODSOFMYCOMPANIONTHATDRMORTIMERISASUCCESSFULELDERLYMEDICALMANWELLESTEEMEDSINCETHOSEWHOKNOWHIMGIVEHIMTHISMARKOFTHEIRAPPRECIATIONGOODSAIDHOLMESEXCELLENTITHINKALSOTHATTHEPROBABILITYISINFAVOUROFHISBEINGACOUNTRYPRACTITIONERWHODOESAGREATDEALOFHISVISITINGONFOOTWHYSOBECAUSETHISSTICKTHOUGHORIGINALLYAVERYHANDSOMEONEHASBEENSOKNOCKEDABOUTTHATICANHARDLYIMAGINEATOWNPRACTITIONERCARRYINGITTHETHICKIRONFERRULEISWORNDOWNSOITISEVIDENTTHATHEHASDONEAGREATAMOUNTOFWALKINGWITHITPERFECTLYSOUNDSAIDHOLMESANDTHENAGAINTHEREISTHEFRIENDSOFTHECCHISHOULDGUESSTHATTOBETHESOMETHINGHUNTTHELOCALHUNTTOWHOSEMEMBERSHEHASPOSSIBLYGIVENSOMESURGICALASSISTANCEANDWHICHHASMADEHIMASMALLPRESENTATIONINRETURNREALLYWATSONYOUEXCELYOURSELFSAIDHOLMESPUSHINGBACKHISCHAIRANDLIGHTINGACIGARETTEIAMBOUNDTOSAYTHATINALLTHEACCOUNTSWHICHYOUHAVEBEENSOGOODASTOGIVEOFMYOWNSMALLACHIEVEMENTSYOUHAVEHABITUALLYUNDERRATEDYOUROWNABILITIESITMAYBETHATYOUARENOTYOURSELFLUMINOUSBUTYOUAREACONDUCTOROFLIGHTSOMEPEOPLEWITHOUTPOSSESSINGGENIUSHAVEAREMARKABLEPOWEROFSTIMULATINGITICONFESSMYDEARFELLOWTHATIAMVERYMUCHINYOURDEBTHEHADNEVERSAIDASMUCHBEFOREANDIMUSTADMITTHATHISWORDSGAVEMEKEENPLEASUREFORIHADOFTENBEENPIQUEDBYHISINDIFFERENCETOMYADMIRATIONANDTOTHEATTEMPTSWHICHIHADMADETOGIVEPUBLICITYTOHISMETHODSIWASPROUDTOOTOTHINKTHATIHADSOFARMASTEREDHISSYSTEMASTOAPPLYITINAWAYWHICHEARNEDHISAPPROVALHENOWTOOKTHESTICKFROMMYHANDSANDEXAMINEDITFORAFEWMINUTESWITHHISNAKEDEYESTHENWITHANEXPRESSIONOFINTERESTHELAIDDOWNHISCIGARETTEANDCARRYINGTHECANETOTHEWINDOWHELOOKEDOVERITAGAINWITHACONVEXLENSINTERESTINGTHOUGHELEMENTARYSAIDHEASHERETURNEDTOHISFAVOURITECORNEROFTHESETTEETHEREARECERTAINLYONEORTWOINDICATIONSUPONTHESTICKITGIVESUSTHEBASISFORSEVERALDEDUCTIONSHASANYTHINGESCAPEDMEIASKEDWITHSOMESELFIMPORTANCEITRUSTTHATTHEREISNOTHINGOFCONSEQUENCEWHICHIHAVEOVERLOOKEDIAMAFRAIDMYDEARWATSONTHATMOSTOFYOURCONCLUSIONSWEREERRONEOUSWHENISAIDTHATYOUSTIMULATEDMEIMEANTTOBEFRANKTHATINNOTINGYOURFALLACIESIWASOCCASIONALLYGUIDEDTOWARDSTHETRUTHNOTTHATYOUAREENTIRELYWRONGINTHISINSTANCETHEMANISCERTAINLYACOUNTRYPRACTITIONERANDHEWALKSAGOODDEALTHENIWASRIGHTTOTHATEXTENTBUTTHATWASALLNONOMYDEARWATSONNOTALLBYNOMEANSALLIWOULDSUGGESTFOREXAMPLETHATAPRESENTATIONTOADOCTORISMORELIKELYTOCOMEFROMAHOSPITALTHANFROMAHUNTANDTHATWHENTHEINITIALSCCAREPLACEDBEFORETHATHOSPITALTHEWORDSCHARINGCROSSVERYNATURALLYSUGGESTTHEMSELVESYOUMAYBERIGHTTHEPROBABILITYLIESINTHATDIRECTIONANDIFWETAKETHISASAWORKINGHYPOTHESISWEHAVEAFRESHBASISFROMWHICHTOSTARTOURCONSTRUCTIONOFTHISUNKNOWNVISITORWELLTHENSUPPOSINGTHATCCHDOESSTANDFORCHARINGCROSSHOSPITALWHATFURTHERINFERENCESMAYWEDRAWDONONESUGGESTTHEMSELVESYOUKNOWMYMETHODSAPPLYTHEMICANONLYTHINKOFTHEOBVIOUSCONCLUSIONTHATTHEMANHASPRACTISEDINTOWNBEFOREGOINGTOTHECOUNTRYITHINKTHATWEMIGHTVENTUREALITTLEFARTHERTHANTHISLOOKATITINTHISLIGHTONWHATOCCASIONWOULDITBEMOSTPROBABLETHATSUCHAPRESENTATIONWOULDBEMADEWHENWOULDHISFRIENDSUNITETOGIVEHIMAPLEDGEOFTHEIRGOODWILLOBVIOUSLYATTHEMOMENTWHENDRMORTIMERWITHDREWFROMTHESERVICEOFTHEHOSPITALINORDERTOSTARTAPRACTICEFORHIMSELFWEKNOWTHEREHASBEENAPRESENTATIONWEBELIEVETHEREHASBEENACHANGEFROMATOWNHOSPITALTOACOUNTRYPRACTICEISITTHENSTRETCHINGOURINFERENCETOOFARTOSAYTHATTHEPRESENTATIONWASONTHEOCCASIONOFTHECHANGEITCERTAINLYSEEMSPROBABLENOWYOUWILLOBSERVETHATHECOULDNOTHAVEBEENONTHESTAFFOFTHEHOSPITALSINCEONLYAMANWELLESTABLISHEDINALONDONPRACTICECOULDHOLDSUCHAPOSITIONANDSUCHAONEWOULDNOTDRIFTINTOTHECOUNTRYWHATWASHETHENIFHEWASINTHEHOSPITALANDYETNOTONTHESTAFFHECOULDONLYHAVEBEENAHOUSESURGEONORAHOUSEPHYSICIANLITTLEMORETHANASENIORSTUDENTANDHELEFTFIVEYEARSAGOTHEDATEISONTHESTICKSOYOURGRAVEMIDDLEAGEDFAMILYPRACTITIONERVANISHESINTOTHINAIRMYDEARWATSONANDTHEREEMERGESAYOUNGFELLOWUNDERTHIRTYAMIABLEUNAMBITIOUSABSENTMINDEDANDTHEPOSSESSOROFAFAVOURITEDOGWHICHISHOULDDESCRIBEROUGHLYASBEINGLARGERTHANATERRIERANDSMALLERTHANAMASTIFFILAUGHEDINCREDULOUSLYASSHERLOCKHOLMESLEANEDBACKINHISSETTEEANDBLEWLITTLEWAVERINGRINGSOFSMOKEUPTOTHECEILINGASTOTHELATTERPARTIHAVENOMEANSOFCHECKINGYOUSAIDIBUTATLEASTITISNOTDIFFICULTTOFINDOUTAFEWPARTICULARSABOUTTHEMANSAGEANDPROFESSIONALCAREERFROMMYSMALLMEDICALSHELFITOOKDOWNTHEMEDICALDIRECTORYANDTURNEDUPTHENAMETHEREWERESEVERALMORTIMERSBUTONLYONEWHOCOULDBEOURVISITORIREADHISRECORDALOUDMORTIMERJAMESMRCSGRIMPENDARTMOORDEVONHOUSESURGEONFROMTOATCHARINGCROSSHOSPITALWINNEROFTHEJACKSONPRIZEFORCOMPARATIVEPATHOLOGYWITHESSAYENTITLEDISDISEASEAREVERSIONCORRESPONDINGMEMBEROFTHESWEDISHPATHOLOGICALSOCIETYAUTHOROFSOMEFREAKSOFATAVISMLANCETDOWEPROGRESSJOURNALOFPSYCHOLOGYMARCHMEDICALOFFICERFORTHEPARISHESOFGRIMPENTHORSLEYANDHIGHBARROWNOMENTIONOFTHATLOCALHUNTWATSONSAIDHOLMESWITHAMISCHIEVOUSSMILEBUTACOUNTRYDOCTORASYOUVERYASTUTELYOBSERVEDITHINKTHATIAMFAIRLYJUSTIFIEDINMYINFERENCESASTOTHEADJECTIVESISAIDIFIREMEMBERRIGHTAMIABLEUNAMBITIOUSANDABSENTMINDEDITISMYEXPERIENCETHATITISONLYANAMIABLEMANINTHISWORLDWHORECEIVESTESTIMONIALSONLYANUNAMBITIOUSONEWHOABANDONSALONDONCAREERFORTHECOUNTRYANDONLYANABSENTMINDEDONEWHOLEAVESHISSTICKANDNOTHISVISITINGCARDAFTERWAITINGANHOURINYOURROOMANDTHEDOGHASBEENINTHEHABITOFCARRYINGTHISSTICKBEHINDHISMASTERBEINGAHEAVYSTICKTHEDOGHASHELDITTIGHTLYBYTHEMIDDLEANDTHEMARKSOFHISTEETHAREVERYPLAINLYVISIBLETHEDOGSJAWASSHOWNINTHESPACEBETWEENTHESEMARKSISTOOBROADINMYOPINIONFORATERRIERANDNOTBROADENOUGHFORAMASTIFFITMAYHAVEBEENYESBYJOVEITISACURLYHAIREDSPANIELHEHADRISENANDPACEDTHEROOMASHESPOKENOWHEHALTEDINTHERECESSOFTHEWINDOWTHEREWASSUCHARINGOFCONVICTIONINHISVOICETHATIGLANCEDUPINSURPRISEMYDEARFELLOWHOWCANYOUPOSSIBLYBESOSUREOFTHATFORTHEVERYSIMPLEREASONTHATISEETHEDOGHIMSELFONOURVERYDOORSTEPANDTHEREISTHERINGOFITSOWNERDONTMOVEIBEGYOUWATSONHEISAPROFESSIONALBROTHEROFYOURSANDYOURPRESENCEMAYBEOFASSISTANCETOMENOWISTHEDRAMATICMOMENTOFFATEWATSONWHENYOUHEARASTEPUPONTHESTAIRWHICHISWALKINGINTOYOURLIFEANDYOUKNOWNOTWHETHERFORGOODORILLWHATDOESDRJAMESMORTIMERTHEMANOFSCIENCEASKOFSHERLOCKHOLMESTHESPECIALISTINCRIMECOMEINTHEAPPEARANCEOFOURVISITORWASASURPRISETOMESINCEIHADEXPECTEDATYPICALCOUNTRYPRACTITIONERHEWASAVERYTALLTHINMANWITHALONGNOSELIKEABEAKWHICHSHOTOUTBETWEENTWOKEENGREYEYESSETCLOSELYTOGETHERANDSPARKLINGBRIGHTLYFROMBEHINDAPAIROFGOLDRIMMEDGLASSESHEWASCLADINAPROFESSIONALBUTRATHERSLOVENLYFASHIONFORHISFROCKCOATWASDINGYANDHISTROUSERSFRAYEDTHOUGHYOUNGHISLONGBACKWASALREADYBOWEDANDHEWALKEDWITHAFORWARDTHRUSTOFHISHEADANDAGENERALAIROFPEERINGBENEVOLENCEASHEENTEREDHISEYESFELLUPONTHESTICKINHOLMESSHANDANDHERANTOWARDSITWITHANEXCLAMATIONOFJOYIAMSOVERYGLADSAIDHEIWASNOTSUREWHETHERIHADLEFTITHEREORINTHESHIPPINGOFFICEIWOULDNOTLOSETHATSTICKFORTHEWORLDAPRESENTATIONISEESAIDHOLMESYESSIRFROMCHARINGCROSSHOSPITALFROMONEORTWOFRIENDSTHEREONTHEOCCASIONOFMYMARRIAGEDEARDEARTHATSBADSAIDHOLMESSHAKINGHISHEADDRMORTIMERBLINKEDTHROUGHHISGLASSESINMILDASTONISHMENTWHYWASITBADONLYTHATYOUHAVEDISARRANGEDOURLITTLEDEDUCTIONSYOURMARRIAGEYOUSAYYESSIRIMARRIEDANDSOLEFTTHEHOSPITALANDWITHITALLHOPESOFACONSULTINGPRACTICEITWASNECESSARYTOMAKEAHOMEOFMYOWNCOMECOMEWEARENOTSOFARWRONGAFTERALLSAIDHOLMESANDNOWDRJAMESMORTIMERMISTERSIRMISTERAHUMBLEMRCSANDAMANOFPRECISEMINDEVIDENTLYADABBLERINSCIENCEMRHOLMESAPICKERUPOFSHELLSONTHESHORESOFTHEGREATUNKNOWNOCEANIPRESUMETHATITISMRSHERLOCKHOLMESWHOMIAMADDRESSINGANDNOTNOTHISISMYFRIENDDRWATSONGLADTOMEETYOUSIRIHAVEHEARDYOURNAMEMENTIONEDINCONNECTIONWITHTHATOFYOURFRIENDYOUINTERESTMEVERYMUCHMRHOLMESIHADHARDLYEXPECTEDSODOLICHOCEPHALICASKULLORSUCHWELLMARKEDSUPRAORBITALDEVELOPMENTWOULDYOUHAVEANYOBJECTIONTOMYRUNNINGMYFINGERALONGYOURPARIETALFISSUREACASTOFYOURSKULLSIRUNTILTHEORIGINALISAVAILABLEWOULDBEANORNAMENTTOANYANTHROPOLOGICALMUSEUMITISNOTMYINTENTIONTOBEFULSOMEBUTICONFESSTHATICOVETYOURSKULLSHERLOCKHOLMESWAVEDOURSTRANGEVISITORINTOACHAIRYOUAREANENTHUSIASTINYOURLINEOFTHOUGHTIPERCEIVESIRASIAMINMINESAIDHEIOBSERVEFROMYOURFOREFINGERTHATYOUMAKEYOUROWNCIGARETTESHAVENOHESITATIONINLIGHTINGONETHEMANDREWOUTPAPERANDTOBACCOANDTWIRLEDTHEONEUPINTHEOTHERWITHSURPRISINGDEXTERITYHEHADLONGQUIVERINGFINGERSASAGILEANDRESTLESSASTHEANTENNAEOFANINSECTHOLMESWASSILENTBUTHISLITTLEDARTINGGLANCESSHOWEDMETHEINTERESTWHICHHETOOKINOURCURIOUSCOMPANIONIPRESUMESIRSAIDHEATLASTTHATITWASNOTMERELYFORTHEPURPOSEOFEXAMININGMYSKULLTHATYOUHAVEDONEMETHEHONOURTOCALLHERELASTNIGHTANDAGAINTODAYNOSIRNOTHOUGHIAMHAPPYTOHAVEHADTHEOPPORTUNITYOFDOINGTHATASWELLICAMETOYOUMRHOLMESBECAUSEIRECOGNISETHATIAMMYSELFANUNPRACTICALMANANDBECAUSEIAMSUDDENLYCONFRONTEDWITHAMOSTSERIOUSANDEXTRAORDINARYPROBLEMRECOGNISINGASIDOTHATYOUARETHESECONDHIGHESTEXPERTINEUROPEINDEEDSIRMAYIINQUIREWHOHASTHEHONOURTOBETHEFIRSTASKEDHOLMESWITHSOMEASPERITYTOTHEMANOFPRECISELYSCIENTIFICMINDTHEWORKOFMONSIEURBERTILLONMUSTALWAYSAPPEALSTRONGLYTHENHADYOUNOTBETTERCONSULTHIMISAIDSIRTOTHEPRECISELYSCIENTIFICMINDBUTASAPRACTICALMANOFAFFAIRSITISACKNOWLEDGEDTHATYOUSTANDALONEITRUSTSIRTHATIHAVENOTINADVERTENTLYJUSTALITTLESAIDHOLMESITHINKDRMORTIMERYOUWOULDDOWISELYIFWITHOUTMOREADOYOUWOULDKINDLYTELLMEPLAINLYWHATTHEEXACTNATUREOFTHEPROBLEMISINWHICHYOUDEMANDMYASSISTANCE