ExCriptoAulas3.mws

TEORIA DOS NÚMEROS E CRIPTOGRAFIA - 2003/04

...ENIGMA...


restart:

with(StringTools):with(group):

Mg:="ELES NAO SABEM NEM SONHAM":
Mg:=SubstituteAll(Mg, " ", "" );
numlet:=nops(convert(Mg,bytes));

Rotores e Reflectores


rotorI:=[5,11,13,6,12,7,4,17,22,26,14,20,15,23,25,8,24,21,19,16,1,9,2,18,3,10]:
Implode(map(Char,rotorI+[seq(64,i=1..26)]));

#                              ABCDEFGHIJKLMNOPQRSTUVWXYZ
rotorI   :=map(Ord-64,Explode("EKMFLGDQVZNTOWYHXUSPAIBRCJ")):
rotorII  :=map(Ord-64,Explode("AJDKSIRUXBLHWTMCQGZNPYFVOE")):
rotorIII :=map(Ord-64,Explode("BDFHJLCPRTXVZNYEIWGAKMUSQO")):
rotorIV  :=map(Ord-64,Explode("ESOVPZJAYQUIRHXLNFTGKDCMWB")):
rotorV   :=map(Ord-64,Explode("VZBRGITYUPSDNHLXAWMJQOFECK")):
rotorVI  :=map(Ord-64,Explode("JPGVOUMFYQBENHZRDKASXLICTW")):
rotorVII :=map(Ord-64,Explode("NZJHGRCXMYSWBOUFAIVLPEKQDT")):
rotorVIII:=map(Ord-64,Explode("FKQHTLXOCBJSPDZRAMEWNIUYGV")):
#
#  Os inversos...
#
rotorIi   :=convert(invperm(convert(rotorI,'disjcyc')),'permlist',26):
rotorIIi  :=convert(invperm(convert(rotorII,'disjcyc')),'permlist',26):
rotorIIIi :=convert(invperm(convert(rotorIII,'disjcyc')),'permlist',26):
rotorIVi  :=convert(invperm(convert(rotorIV,'disjcyc')),'permlist',26):
rotorVi   :=convert(invperm(convert(rotorV,'disjcyc')),'permlist',26):
rotorVIi  :=convert(invperm(convert(rotorVI,'disjcyc')),'permlist',26):
rotorVIIi :=convert(invperm(convert(rotorVII,'disjcyc')),'permlist',26):
rotorVIIIi:=convert(invperm(convert(rotorVIII,'disjcyc')),'permlist',26):


reflectorB:="AY BR CU DH EQ FS GL IP JX KN MO TZ VW":
reflB:=SubstituteAll(reflectorB," ",""):
reflectorB:=[]:
for i from 1 to 13 do
reflectorB:=[op(reflectorB),[Ord(reflB[2*i-1])-64,Ord(reflB[2*i])-64]]:
od:
reflectorB:=convert(reflectorB,'permlist',26);

reflectorC:="AF BV CP DJ EI GO HY KR LZ MX NW TQ SU":
reflC:=SubstituteAll(reflectorC," ",""):
reflectorC:=[]:
for i from 1 to 13 do
reflectorC:=[op(reflectorC),[Ord(reflC[2*i-1])-64,Ord(reflC[2*i])-64]]:
od:
reflectorC:=convert(reflectorC,'permlist',26):

Algoritmo de encriptação tipo ENIGMA (sem "ring setting"):



#...Escolha do reflector e dos rotores:
#
reflector:=reflectorB:
#
rotorR :=(x,i)->rotorIV[x-1+i mod 26 +1]-1-i mod 26 +1:
rotorM :=(x,i)->rotorII[x-1+i mod 26 +1]-1-i mod 26 +1:
rotorL :=(x,i)->rotorV[x-1+i mod 26 +1]-1-i mod 26 +1:
rotorRi:=(x,i)->rotorIVi[x-1+i mod 26 +1]-1-i mod 26 +1:
rotorMi:=(x,i)->rotorIIi[x-1+i mod 26 +1]-1-i mod 26 +1:
rotorLi:=(x,i)->rotorVi[x-1+i mod 26 +1]-1-i mod 26 +1:
#
#...Posiçao inicial dos rotores:
#
kL:=Ord("M")-64: kM:=Ord("A")-64: kR:=Ord("R")-64: 
#
#...The plugboard...
#
pg:="DN GR IS KC QX TM PV HY FW BJ": 
pg:=SubstituteAll(pg," ",""):
plugb:=[]:
for i from 1 to 10 do
plugb:=[op(plugb),[Ord(pg[2*i-1])-64,Ord(pg[2*i])-64]]:
od:
plugb:=convert(plugb,'permlist',26);


convert(plugb,'disjcyc');


#
#...A mensagem...
#
Mg:="XUJWTYMKGMYTGBNJQYRIYGLETSEKUYPBVFWRIPXYFFFZDGKECBNLBWK": numlet:=nops(convert(Mg,bytes));

#
#...Calculo do criptograma:
#
Cript:="": jM:=0: jL:=0:
for i from 1 to numlet do
kR:=kR+1: 
 if floor(kR/26)>jM then jM:=jM+1: kM:=kM+1: fi:
 if floor(kL/26)>jL then jL:=jL+1: kL:=kL+1: fi: 
clet:=plugb[
        rotorRi(
          rotorMi(
            rotorLi(
              reflector[
                rotorL(
                  rotorM(
                    rotorR(plugb[convert(Mg[i],bytes)[1]-64],kR) 
                  ,kM)
                ,kL)
              ]
            ,kL)
          ,kM)
        ,kR)]:
  Cript:=cat(Cript,convert([clet+64],bytes)):
od:
printf("%A",Cript);