fork download
  1. #!/bin/dev/python
  2. # --*--coding:UTF-8--*--
  3.  
  4. from decimal import Decimal
  5. import re
  6.  
  7. def verifMatrix(inv):
  8. # verif : (ad-bc) %2 == 1 and pas multiple de 13
  9. res = False
  10. if (inv %2) == 1 :
  11. if (inv %13) != 0 :
  12. res = True
  13. return res
  14.  
  15. def inverse(k,mod):
  16. l=[i for i in range(mod) if i%2==1 and i!=13]
  17. res = 0
  18. for i in l:
  19. r=(k*i)%mod
  20. if r == 1:
  21. res = i
  22. return res
  23.  
  24. ## coef pour inverser matrice
  25. def dett(m):
  26. inversible = ((m[0][0]*m[1][1])-(m[0][1]*m[1][0]))
  27. return inversible
  28.  
  29. def strToList(string,step):
  30. return [string[w:w+step] if len(string[w:w+step])==step else string[w:w+step]+"X"*(step-len(string[w:w+step])) for w in range(0,len(string),step)]
  31.  
  32. def lettre(dico,id):
  33. return dico.keys()[dico.values().index(id)]
  34.  
  35. ## matrice inverse
  36. def minv(m, inv):
  37. a,b,c,d=m[0][0],m[0][1],m[1][0],m[1][1]
  38. res=[[(d*inv)%26,(-b*inv)%26],[(-c*inv)%26,(a*inv)%26]]
  39. return res
  40.  
  41. ### declaration des variables ########################
  42. alpha=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  43. dico={"A":1,"B":2,"C":3,"D":4,"E":5,"F":6,"G":7,"H":8,"I":9,"J":10,"K":11,"L":12,"M":13,"N":14,"O":15,"P":16,"Q":17,"R":18,"S":19,"T":20,"U":21,"V":22,"W":23,"X":24,"Y":25,"Z":26}
  44.  
  45. cipher1="QEGQE YWAU EZDM KQWPVQWY IWLBGFF JU DHXLY JFM NTUQDJX PJ AZC FZ WS LTP CQC" # 0.54 jlhq
  46. ######## comme quoi avec quelques efforts on finit par toucher au but ne me dis pas
  47. cipher2="QEGQ"
  48. cipher7="DO OVWE MV RK EPGW OVWE WF WNCORWIIER KFTOPO KP AO OVOS MSKNKTEE MSIVGPKNCZWHB IMFXZ WF IX JAMRKMGWEE" # 0.07 vtzc
  49. ######## au fait tu as bien fait de poursuivre jusque la il faut inverser invinoveritas avant de le concatener
  50.  
  51. m1=[[9,11],[7,16]]
  52. m7=[[21,19],[25,2]]
  53.  
  54. ########## main ###################################
  55. cipher=strToList(cipher2.replace(" ",""),2)
  56. m=m1
  57. mod=26
  58. det=dett(m)
  59. invers=inverse(det,mod)
  60. if (verifMatrix(det) and (invers != 0)) :
  61. phrase=""
  62. phrase2=""
  63. matrixInv=minv(m,invers)
  64. a1,b1,c1,d1=matrixInv[0][0],matrixInv[0][1],matrixInv[1][0],matrixInv[1][1]
  65. for i in cipher:
  66. bm=i
  67. l0=ord(bm[0])-65 ## A=0
  68. n0=dico[bm[0]]-1
  69. l1=ord(bm[1])-65 ## A=0
  70. n1=dico[bm[1]]-1
  71. #l0=ord(bm[0])-64 ## A=1
  72. #l1=ord(bm[1])-64 ## A=1
  73. lettre1=(a1*l0+b1*l1)%mod
  74. lettre2=(c1*l0+d1*l1)%mod
  75. nettre1=(a1*n0+b1*n1)%mod
  76. nettre2=(c1*n0+d1*n1)%mod
  77. phrase=phrase+chr(lettre1+97)+chr(lettre2+97) ## A=0
  78. phrase2+=lettre(dico,nettre1+1)+lettre(dico,nettre2+1) ## A=0
  79. # phrase=phrase+chr(lettre1+96)+chr(lettre2+96) ## A=1
  80. print matrixInv
  81. print chr(a1+64)+chr(b1+64)+chr(c1+64)+chr(d1+64)
  82. print phrase
  83. print phrase2
Success #stdin #stdout 0.02s 8516KB
stdin
Standard input is empty
stdout
[[8, 1], [3, 11]]
HACK
comm
COMM