SHARE
TWEET

Untitled

a guest Jun 25th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FILE = "C:/Users/Asus/Desktop/GEDEFU.txt"
  2. charset = ["A", "D", "G", "F","V", "X"]
  3. french = [" ", "E", "A", "I", "S", "N", "R", "T", "O", "L", "U", "D", "C", "M", "P", "G", "B", "V", "H", "F", "Q", "Y", "X", "K", "W","Z", "1", "9", "6", "2", "8", "3", "4", "5", "7", "0"]
  4. clef = "PAHMVIN"
  5.  
  6. def padding(content):
  7.     if len(content)%2 != 0:
  8.         content += "X"
  9.     return content
  10.  
  11. def transposition(key, content):
  12.     matrice, transposition = [[0]*373 for i in range(len(key))], [[0]*len(key) for i in range(373)]
  13.     tab, dic, inter = list(zip(*[iter(content)]*len(key))), {}, []
  14.     i,j = 0, 0
  15.     for i in range(len(key)):
  16.         for j in range(373):
  17.             matrice[i][j] = tab[j][i]
  18.     decomp_key = list(key)
  19.     decomp_key_al = sorted(decomp_key, reverse=True)
  20.     for i in range(len(decomp_key_al)):
  21.         dic[decomp_key_al[i]] = matrice[i]
  22.     for elt in decomp_key:
  23.         inter.append(dic[elt])
  24.     for i in range(len(inter)):
  25.         for j in range(len(transposition)):
  26.             transposition[j][i] = inter[i][j]
  27.     return transposition
  28.  
  29. def convertString(array, n, p):
  30.     transpo = ""
  31.     for i in range(n):
  32.         for j in range(p):
  33.             transpo += array[j][i]
  34.     return transpo
  35.  
  36. def refe(array):
  37.     ref, i, j = [], 0, 0
  38.     for i in range(len(array)):
  39.         for j in range(len(array)):
  40.             ref.append(array[i]+array[j])
  41.     return ref
  42.  
  43. def getblock(content):
  44.     i,solo,block = 0,[], []
  45.     content = padding(content)
  46.     solo = list(content)
  47.     while i != len(solo):
  48.         block.append(solo[i]+solo[i+1])
  49.         i += 2
  50.     return block
  51.  
  52. def occurence(block, ref): #Calcule la fréquence d'apparition d'une pair de lettres.
  53.     i, j, count, res = 0, 0, 0, []
  54.     for i in range(len(ref)):
  55.         for j in range(len(block)):
  56.             if block[j] == ref[i]:
  57.                 count += 1
  58.         count = round((count*100)/len(block), 2)
  59.         res.append(count)
  60.         count = 0
  61.     return res
  62.  
  63. def liste(ref, frequence, french):  #Créer une liste avec correspondance.
  64.     liste , i = [], 0
  65.     for i in range(len(ref)):
  66.         elt = ref[i]+":"+str(frequence[i])
  67.         liste.append(elt)
  68.     sub_liste, frequence, i = [], sorted(frequence, reverse=True), 0
  69.     for i in range(len(french)):
  70.         sub_liste.append(liste[i].replace(liste[i][3:], french[i]))
  71.     return sub_liste
  72.  
  73. def substitution(block, liste, ref): #Remplace les lettres dans le texte par les lettres de la frequence de la langue (Etape 1 du chiffrement)
  74.     for i in range(len(ref)):
  75.         for j in range(len(block)):
  76.             if ref[i] == block[j]:
  77.                 block[j] = block[j].replace(block[j], liste[i][3:])
  78.     return block
  79.  
  80. def convertChar(array):
  81.     solve, i = "", 0
  82.     for i in range(len(array)):
  83.         solve += array[i]
  84.     return solve
  85.  
  86.  
  87. def main():
  88.     with open(FILE, "r") as content:
  89.         raw = content.read()
  90.         content.close()
  91.     raw = padding(raw)
  92.     message_reverse = transposition(clef, raw)
  93.     message_reverse = convertString(message_reverse, len(clef), len(message_reverse))
  94.     block = getblock(message_reverse)
  95.     reference = refe(charset)
  96.     frequence = occurence(block, reference)
  97.     correspondance = liste(reference, frequence, french)
  98.     array_solve = substitution(block, correspondance, reference)
  99.     solve = convertChar(array_solve)
  100.     print(len(solve))
  101.  
  102. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top