ExCriptoAulas2.mws

TEORIA DOS NÚMEROS E CRIPTOGRAFIA - 2003/04

...CRIPTOANÁLISE DA CIFRA DE VIGENÈRE...

Algoritmo de encriptação para a cifra de Vigenère:

restart:
M:="eles nao sabem nem sonham que o sonho e uma constante da vida tao concreta e definida como outra coisa qualquer como esta pedra cinzenta em que me sento e descanso como este ribeiro manso em serenos sobressaltos como estes pinheiros altos que em verde e oiro se agitam como estas aves que gritam em bebedeiras de azul eles nao sabem que o sonho e vinho e espuma e fermento bichinho alacre e sedento de focinho pontiagudo que fossa atraves de tudo num perpetuo movimento eles nao sabem que o sonho e tela e cor e pincel base fuste capitel arco em ogiva vitral pinaculo de catedral contraponto sinfonia mascara grega magia que e retorta de alquimista mapa do mundo distante rosa dos ventos infante caravela quinhentista que e cabo da boa esperanca ouro canela marfim florete de espadachim bastidor passo de danca colombina e arlequim passarola voadora para raios locomotiva barco de proa festiva alto forno geradora cisao do atomo radar ultra som televisao desembarque em foguetao na superficie lunar eles nao sabem nem sonham que o sonho comanda a vida que sempre que um homem sonha o mundo pula e avanca como bola colorida entre as maos de uma crianca":m:=nops(convert(M,bytes));
K:="entao": m:=nops(convert(K,bytes)); 
C:="": j:=0:
for i from 1 while evalb(M[i]<>"") do
 if M[i]<>" " then
  j:=j+1: if j=m+1 then j:=1: fi:
  c:=convert(M[i],bytes)[1]+convert(K[j],bytes)[1]-2*97 mod 26 +97:
  C:=cat(C,convert([c],bytes)):
 fi: 
od:
printf("%A",C);
nops(convert(C,bytes));

Teste de Kasiski:

m:=3: lg:=nops(convert(C,bytes)): j:=0:
for i from 1 to lg do
 j:=j+1: if j=m+1 then j:=1: fi:
 printf("%A",C[i]):
 if j=m then printf(" "): fi:
 if frac(i/(m*20))=0 then printf("\n"): fi:
od: 
for i from 3 to lg do
 for j from i+1 to lg do:
  if cat(C[i-2],C[i-1],C[i])= cat(C[j-2],C[j-1],C[j]) then
   bloco:=cat(C[i-2],C[i-1],C[i]): 
    for k from 1 while(C[i+k]=C[j+k]) do
     bloco:=cat(bloco,C[i+k]):
    od:
   if nops(convert(bloco,bytes))>3 then printf("(%A,%A) %A %A\n",i,j,bloco,ifactor(j-i)): i:=i+k:
   fi: 
  fi:
 od:
od: 

Algoritmo de decifração da cifra de Vigenère:

#K:="entao": 
m:=nops(convert(K,bytes)):
M:=C: Dc:="":
for i from 1 while evalb(M[i]<>"") do
 j:=i mod m: 
  if j=0 then j:=m:fi:
c:=convert(M[i],bytes)[1]-convert(K[j],bytes)[1] mod 26 +97: #...97->minusculas;65->maiusculas
 Dc:=cat(Dc,convert([c],bytes)): 
od:
printf("%A",Dc);

Frequência das letras e índice de coincidência:

#C:=M:
m:=nops(convert(C,bytes)):
ind:=0:
for j from 0 to 25 do:
c:=0: 
for i from 1 to m do:
 if C[i]=convert([97+j],bytes) then c:=c+1: fi:
od:
printf("(%A) %A\n",convert([97+j],bytes),evalf(c/m)*100): ind:=ind+(c/m)^2: 
od:
printf("indice de coincidencia = %A\n",evalf(ind)):
interface(displayprecision=5);evalf(ind);
ICportugues:=0.07813849:
ICfrances:=0.0778:
ICespanhol:=0.0775:
ICalemao:=0.0762:
ICitaliano:=0.0738:
ICingles:= 0.0667: # 0.065 e' tambem citado...
ICrusso:= 0.0529: # O alfabeto russo tem 30 caracteres...
l:=evalf((ICportugues-1/26)*m/(ind*(m-1)-(1/26*m-ICportugues)));

*******************************************************************************

Um exercício -- decifrar a seguinte mensagem (encriptada com a cifra de Vigenère):

C:="uszhmgvgldwnbygwezhdgrbkiswrpsvinnokxbkouseyefxrhngeyptwvnaujeoyakmylijespcgybyfsebirshblsuvvaojfnpafsrvrwxehtghbirswvsdgtbcozyzplviqvnwkevdsqhshwvflnkynsexiypzfefjivsnkerhrhggwgvdwqvsngzrjefxbzevsmlnsjngefhnhujmppdaeavajvnpadhrmejvnkakhvztjmgvdgqhuiumcpovivaatyahjgvtljeeqvejebwraqbnefmgvdwnbhosqnkovishraerludeypadinsaeeqvdwjnyisnbygwezhdgifaenirtfgvghlwdnlmemyuoniplnlsflngzrutsippnuschrssyhnuezlnlsqlavifjotieaaventejmphpwpbztmvpvsfsplnlvbkeusacefgblsxmyhswrbymwwqllwmgvrwwflfgvzhrsqchrsgbuswkhprshrkiuegvraeqvamxbyohyosiusflnlmnxuwefhuviqllwrnvejentekqnhahifhrvetyafhrsuumqlzwgbusumrucaeqvswychpwpqlekgeptgvrkaxmetereqvswyfvcaeypseshaohmpvauvrzcahbkemqbaiemftouszvfmxhyovsoyakmyxuwhrpxgygyafwchrwgrynswfbakiaarwzvztswwvryiflmhvrhcjiqptgyavbjefplwrblsuvvaojfehsaprprgefbagfehegvrarsxbkemqchikuhlswhvgmghryngqnzdwyzhdazrysahnkeuyyaujeylngvzlfsgbjojsnzpspncrswqlfsfvvlmgnznszvzaggeptagnkotvnzidnbygwezhdgxehbspuhcgqnjofxehpgwvjaglvztgvvjavsoyafgbwejeaaegrrnrgibmejiplcgqbyektbztstbzilmihoaqrusgifausvvvdsqvzcakruauebuekwrhshipaogifjraxbybsmnuokizwrwespreshjoelhtidhnkeiyrhpwrnzotwryvsznhshifzoswruagxvuhsteltwrfhovirepdmphrswbjiwhnkeiynudgwrmadeahvswghotvnkosygvrdstvswgepamqnpmskrtdwqnnnaxhkevefwejwbuayiazfwqvuifefhpjsoseeegpcshntudlrybgrvaakiazuspgyafwtyekwbyawmeyeiyvltsuhlswifaatiylcwrbpmskvuajmbkoyvnudwthilagbseaxbypjmajiheytefxrhpsvgprvithbjmrsauvncowgnuedezplfsilcwrgvswgvuqmiaaawsvaohevzdggnynsznsmapavvwgrutgwrararghemqrhekxelispvaejeepavsrzcjmgvriyrjaemaoomtnyagvbtafgrkeahrvlgkvhpgpvaiuepvmsthilagnjaghrjauehtidrbceuiaaokigyifxnltjifhgjeakeyyvuaveqhvahnldssoyasqnkisrnzevepvmgeohnvsavdgtnytahbjoeyapslerkojszhnuiqlefknqaeiaaohsyptagbhotvnkebsenesqnkoheeaeheehuegnzaeiaaouszhajxrwrgtepaeiaaevmghekwrlnusaargislilspvmteflnswclrksahgwrfmeemapnswntudlrypswfvuswryowtvjefxevdsrnyrsxvcastnytavqhcjmnjaghrnatvvllsuhlpgwglrasetefxrvlwznyisepyisvqvnsjyvrlielssfnaikxnltaighdwrgyovecyotprtalmphdgvbtafgrtovieuotvnzidivyoyeoyiwpnaoeenzswrgvngqhudgjvjcasahlssyhdghrprsgrtakiaoojenlsuvncaawnbrsgnwilypvnuivjagizbilsfvulvbzmaxbzqminmiugnvbjefplwmehcjmbbagpbugghbztwqcvsgwnzpwggvsdmepcgwbhulsetuaxbieeiazaaeeheeqnymgvgvmapavvwgrutgwrararghekivzqmeakouvvvudmipaeeflsgqrutwechrlmekeyeoyiwpnxuwsqpmwrfpofezlnlsqvfwqvuifsflfabnuojszhnuintavmnuowegyanifkekwnvbjedbegwcvslyyhdgwcvlaxvjokwnvpgwgvsviyhdgtnyaveesuyeehueerztjygbrsrnyrsxvcaeevzedeovrshnnatvvllsrbkiriekouvvaiuschudschekrnvdwmkhdwwrylavvjahefaojiyhpgvrtdwyzhlavvjaesqlrfeqlsjszhnlmmhdstepnumchleiaaefsdbevmmyektrptgebhshipaoksppadeboueselakegprsgbtpsvnyahvbaaysapslenbmstnztgvnlswqqbvahnwedephnvmqlziyrcikphtbjeqvaheelcaqrutghnwejwbuayizjoewrbpmieplvifsuefehmwrgvfwiepcghbjijgbtuaxbjoeyzuoeyakovepbllyehpgthsajzrqaesfjoesnwajiplgsfepedecllstepmwmehvwdqpafxrkefeppbnifaiveqltjecvsemflrszrpsusolrleqltsqnuhswhqeavnxuwiehietbzsazrsvwvyoeswsliusrzeveeshwmqhdwsfjatiyvsvifnrwruhdgwvtufhbzdwtbvshifkekgnscgwavdwgbyrwvqhnsvehtaznhpjsghggrvztsipvmheehdsehtaxpbyrgwnyutvnjoegulijsqlcjeivpwvsbmwhrnatvvllsgbtokipvnkxnaaggbuvarplnlieladmftowqppruyaztsrppavmilrkeqvlavvzmgtnztgvrsakirciviajisvnjoesqlswrivlnmzlnlsqhnsvehtaznlswviprshrjofxehslirutjibsijmpvegwrusmeyxuwephbjmghdgwrytssrmrmxbkoskelslirwonsahoksbhmgvqlnsgvipgvthbjmrsaliaaalvnusxsetadertueeqhmshnzoumrkaviflmjifblleqvseefhcaqnkelyqvafeeyalmihcgrghazmfaojmnkadmolrveqldsgepalyehhmqnuauszvswvhuiusrpnvippfjeillbeqpzaebjraxvjolvvztssqlallnfdweupslsepavercodyphoksppadhrplzihznssrztshvzsggvhdshnoikxbyishnlvgphjagwbjispqlgsfepederuqmeaaoapulukwrnuwrnzusfepgstrsohsqlrsxehvwwqvcgrsyofxbkokgbyofivzehsyptagbzqmielpjiflnlezvpswfhdgibmulyevgsfepedeflgmiypvjizlnlipvmgqbjakizmaemypawjvshshbuavedbevifljseclnswbztsxhzdwezhnlintaverjojxrqaveflmiyrpsksphslvrhsmefldwhrsitiekavipvmgyapcsjbycswvutwxvgavsehdgwrbivinsdwzvkagxehtsqrutghnkossntojiznatvvllsgehvgiphnwpnzejzrzeehhcivechrsnhztajvjajsubmgvrvejsgpsescyekiaaekrnvbjebwrgkelsksqlidlrbskmtuixmphohvbnrwwfvdgxehtsqrutghnkohiyvhgqrtaeyyoejinvaeseuojszhnuibzcswbzaesevsgwfholvnaavsfkeesqvektrjisppvmwgnudgtbyoxiapsaenwaabbuavecllgmzwejeqvrhefzafhbwedsevmsrplpswfpofeykekmaoarmaoawsftufhblvgphpnvspvmsxehiuebkeypbyisibwejhnvswqqbvahnlskidbavvbkeyeylraeqlmmpulrwwipsleflmjiyhcssnvaesezeuszwlwxnyausztadzvuaxmyoavipvrgrrsqmielnwknvdwwgpnghrtouephsshbprshrzaxmnudgsfwrwgbucwmgvswxbynsrqvswehuiueclrksahgwqdbewpniojehtanmfhouvvaiueqlskefvcaiqhdwmyoewrfldgwnuokzvutwzbstsrqvayeoyiwpnhpjsghggrvztstrymsrrjeuszvueeclrksahgwqgllmvvjavivtefwbciysezefwhhlfeahrjegpvsuhlnssflsmfzltweapnyyrtnwqflefuhhdjeahvwpuhomrnuonevshwyfhpwwnydgwrzfgvpvsviahcafrtqmielrlvnusxsetadenualyelzshrnatvvllsintaawnbtwrgpcshraovefhshiezofetlnkhbyoeeajeweglnlegpvshrtuveajalseuasyzhmggnarawgvnzerlnkmzlseeqhokxnaukwbjispnkqmmepdgtrsouefhmwrgvnssglmgqrzmgznsojiyhbgvnkohiyhmgvnsbmvtbekebxuwprcagxretgezhdaeavamqspnspslladdbafhbhpwvfvnskrtdwmkhdwwrydsqnkaksppeveqlensyaaswrysaqcsekwvtbgpbzepynsevrnjajpbzdweuvlsrqhpjsslsksehdsyapvwvfpdshrlsleqbadhbjesvn":
m:=nops(convert(C,bytes));