# probword.py # v1.0, JF Bouchaudy, 20/12/14 import sys import string import math # =================================== def is_maj(car): if string.find(string.ascii_uppercase,car) == -1: return 0 else: return 1 #===========fic2ch ===================== def fic2ch(f): chaine = "" ; lg = 0 while True: ch = f.readline() ligne = "" if ch == "": break for i in range( len(ch) ): if is_maj( ch[i] ): ligne += ch[i] ; lg += 1 chaine += ligne return chaine, lg #======================================= #_____ lecture des crypto et des mots cles ____ if len(sys.argv) < 2 : print "Desc.: " print " A given probable word is placed at each position of the text of " print " the first file and the software tries to decipher the text of " print " the second file at the same position. " print "Syntaxe: probword.py file_1 file_2 probable_word [debug]" print "Example: probword.py f1.txt f2.txt ZPARENZ" sys.exit(1) if len(sys.argv) > 6 : DEBUG = 1 else: DEBUG = 0 fcry1 = open( sys.argv[1], "r") fcry2 = open( sys.argv[2], "r") cry1, lg1 = fic2ch( fcry1 ) cry2, lg2 = fic2ch( fcry2 ) motproba = sys.argv[3] #---- recherche du plus petit lg = lg1 if lg1 > lg2: lg = lg2 cryptos = [] cryptos.append( cry1[:lg] ) cryptos.append( cry2[:lg] ) if DEBUG : print "cryptos: ====> \n", cryptos result = {} for unMotCle in [ motproba ]: cle = [0] * len(unMotCle) for uneLigne in range(1): maxi = 0 for i in range( len(cryptos[0])-len(unMotCle) ): for j in range( len(unMotCle) ): cle[j] = (ord(unMotCle[j])-65 + ord(cryptos[uneLigne][i+j])-65)%26 print unMotCle, uneLigne, i, bouchif = cryptos[uneLigne][i:i+j+1] hypo = "" for j in range( len(unMotCle) ): lettre = string.uppercase.index( cryptos[(uneLigne+1)%2][i+j] ) chiffre = string.uppercase[ (26 + cle[j] - lettre )%26 ] hypo += chiffre print hypo