Esempio n. 1
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()

    for cipherword in cipherwordList:
        # Get a new cipherletter mapping for each ciphertext word:
        candidateMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)

        if wordPattern not in wordPatterns.allPatterns:
            continue  # This word was not in our dictionary, so continue.

        # Add the letters of each candidate to the mapping:
        for candidate in wordPatterns.allPatterns[wordPattern]:
            addLettersToMapping(candidateMap, cipherword, candidate)

        # Intersect the new mapping with the existing intersected mapping:
        intersectedMap = intersectMappings(intersectedMap, candidateMap)

    # Remove any solved letters from the other lists:
    letterMap = removeSolvedLettersFromMapping(intersectedMap)
    hackedMessage = decryptWithCipherletterMapping(message, letterMap)

    op = open('dictionary.txt')
    wl = op.read().split('\n')
    op.close
    while '_' in hackedMessage:
        nonletter = re.compile('[^A-Z_\s]')
        hackList = nonletter.sub('', hackedMessage.upper()).split()
        for i in range(len(hackList)):
            if '_' in hackList[i]:
                numOfBlank = hackList[i].count('_')
                if numOfBlank == 1:
                    index = hackList[i].find('_')
                    values = letterMap.get(cipherwordList[i][index])
                    matchedList = []
                    for value in values:
                        word = hackList[i].replace('_', value)
                        for w in wl:
                            matchW = re.match(word.upper(), w)
                            if matchW:
                                matchedList.append(matchW.group())
                    if len(matchedList) == 1:
                        letterMap[cipherwordList[i][index]] = [
                            matchedList[0][index]
                        ]
                        letterMap = removeSolvedLettersFromMapping(letterMap)

        hackedMessage = decryptWithCipherletterMapping(message, letterMap)

    print('Mapping:')
    print(letterMap)

    return hackedMessage
Esempio n. 2
0
    def run(self):
        for cipherword in self.cipherwordList:
            newMap = getBlankcipherLetterMapping()

            wordPattern = makeWordPatterns.getWordPattern(cipherword)
            if wordPattern not in wordPatterns.allPatterns:
                continue

            # Add the letters of each candidate to the mapping.
            for candidate in wordPatterns.allPatterns[wordPattern]:
                newMap = addLettersToMapping(newMap, cipherword, candidate)
            with self.lock:
                intersectedMap = intersectMappings(intersectedMap, newMap)
Esempio n. 3
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()

    for cipherword in cipherwordList:
        newMap = getBlankCipherletterMapping()
        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue
        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)
        intersectedMap = intersectMappings(intersectedMap, newMap)
    return removeSolvedLettersFromMapping(intersectedMap)
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    
    for cipherword in cipherwordList:
        newMap = getBlankCipherletterMapping()
        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue

        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)

        intersectedMap = intersectMappings(intersectedMap, newMap)
    return removeSolvedLettersFromMapping(intersectedMap)
Esempio n. 5
0
def hackearSubSimples(mensagem):
    mapaCruzado = gerarMapaBranco()
    listaPalavrasCifradas = gerarLPC(mensagem)
    for palavraCifrada in listaPalavrasCifradas:
        mapaCandidatos = gerarMapaBranco()
        padraoPalavra = makeWordPatterns.getWordPattern(palavraCifrada)
        if padraoPalavra not in wordPatterns.allPatterns:
            continue  #se esta palavra não está no dicionário, continue.
        for candidato in wordPatterns.allPatterns[padraoPalavra]:
            adicionarLetrasAoMapeamento(mapaCandidatos, palavraCifrada,
                                        candidato)
        mapaCruzado = cruzarMapas(mapaCruzado, mapaCandidatos)
    removerLetrasResolvidasDoMapeamento(mapaCruzado)

    return mapaCruzado
Esempio n. 6
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    for cipherword in cipherwordList:
        # Get a new cipherletter mapping for each ciphertext word:
        candidateMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue  # This word was not in our dictionary, so continue.

        for candidate in wordPatterns.allPatterns[wordPattern]:
            addLettersToMapping(candidateMap, cipherword, candidate)

        intersectedMap = intersectMappings(intersectedMap, candidateMap)

    return removeSolvedLettersFromMapping(intersectedMap)
Esempio n. 7
0
def hackSimpleSub(message):
    intersectedMap = getBlackCipherLetterMapping()
    cipherWordList = nonLetterRegEx.sub('', message.upper()).split()

    # loop through the list of cipher words
    for cipherWord in cipherWordList:
        candidateMap = getBlackCipherLetterMapping()
        wordPattern = makeWordPatterns.getWordPattern(cipherWord)
        # check that pattern in all the patterns
        if wordPattern not in wordPatterns.allPatterns:
            continue
        # add letters to the map
        for candidate in wordPatterns.allPatterns[wordPattern]:
            addLettersToMapping(candidateMap, cipherWord, candidate)
        # intersect that map
        intersectedMap = intersectMapping(candidateMap, intersectedMap)

    return removeSolvedLetterFromMapping(intersectedMap)
def hack_simple_sub(message):
    intersected_map = get_blank_cipher_letter_mapping()
    cipherword_list = non_letters_or_space_pattern.sub(
        '', message.upper()).split()

    for cipherword in cipherword_list:
        candidate_map = get_blank_cipher_letter_mapping()

        word_pattern = makeWordPatterns.getWordPattern(cipherword)
        if word_pattern not in wordPatterns.allPatterns:
            continue

        for candidate in wordPatterns.allPatterns[word_pattern]:
            add_letters_to_mapping(candidate_map, cipherword, candidate)

        intersected_map = intersect_mappings(intersected_map, candidate_map)

    return remove_solved_letters_from_mapping(intersected_map)
Esempio n. 9
0
113. def hackSimpleSub(message):
114.     intersectedMap = getBlankCipherletterMapping()
115.     cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
116.     for cipherword in cipherwordList:
117.         # Get a new cipherletter mapping for each ciphertext word.
118.         newMap = getBlankCipherletterMapping()
119.
120.         wordPattern = makeWordPatterns.getWordPattern(cipherword)
121.         if wordPattern not in wordPatterns.allPatterns:
122.             continue # This word was not in our dictionary, so continue.
123.
124.         # Add the letters of each candidate to the mapping.
125.         for candidate in wordPatterns.allPatterns[wordPattern]:
126.             newMap = addLettersToMapping(newMap, cipherword, candidate)
127.
128.         # Intersect the new mapping with the existing intersected mapping.
129.         intersectedMap = intersectMappings(intersectedMap, newMap)
130.
131.     # Remove any solved letters from the other lists.
132.     return removeSolvedLettersFromMapping(intersectedMap)
133.
134.
135. def decryptWithCipherletterMapping(ciphertext, letterMapping):
136.     # Return a string of the ciphertext decrypted with the letter mapping,
137.     # with any ambiguous decrypted letters replaced with an _ underscore.
138.
139.     # First create a simple sub key from the letterMapping mapping.
140.     key = ['x'] * len(LETTERS)
141.     for cipherletter in LETTERS:
142.         if len(letterMapping[cipherletter]) == 1:
143.             # If there's only one letter, add it to the key.
144.             keyIndex = LETTERS.find(letterMapping[cipherletter][0])
145.             key[keyIndex] = cipherletter
146.         else:
147.             ciphertext = ciphertext.replace(cipherletter.lower(), '_')
148.             ciphertext = ciphertext.replace(cipherletter.upper(), '_')
149.     key = ''.join(key)
150.
151.     # With the key we've created, decrypt the ciphertext.
152.     return simpleSubCipher.decryptMessage(key, ciphertext)
153.
154.
155. if __name__ == '__main__':
156.     main()
Esempio n. 10
0
def hackSimpleSub(cipherText):
    intersectedMap = getBlankcipherLetterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('',
                                                  cipherText.upper()).split()
    for cipherword in cipherwordList:
        newMap = getBlankcipherLetterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue

        # Add the letters of each candidate to the mapping.
        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)

        intersectedMap = intersectMappings(intersectedMap, newMap)

    # Remove any solved letters from the other lists.
    return removeSolvedLettersFromMapping(intersectedMap)
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('',message.upper()).split()
    for cipherword in cipherwordList:
        # Get a new cipherletter mapping for each ciphertext word.
        newMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue # This word was not in our dictionary, so continue.

        # Add the letters of each candidate to the mapping.
        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)
        # Intersect the new mapping with the existing intersected mapping.
        intersectedMap = intersectMappings(intersectedMap, newMap)

    # Remove any solved letters from the other lists.
    return removeSolvedLettersFromMapping(intersectedMap)
Esempio n. 12
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    for cipherword in cipherwordList:
        # Get a new cipherletter mapping for each ciphertext word.
        newMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue  # This word was not in our dictionary, so continue.

        # Add the letters of each candidate to the mapping.
        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)

        # Intersect the new mapping with the existing intersected mapping.
        intersectedMap = intersectMappings(intersectedMap, newMap)

    # Remove any solved letters from the other lists.
    return removeSolvedLettersFromMapping(intersectedMap)
Esempio n. 13
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherLetterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    for cipherword in cipherwordList:
        # Get new cipherletter mapping for each ciphertext word
        candidateMap = getBlankCipherLetterMapping()

        word_pattern = makeWordPatterns.getWordPattern(cipherword)
        if word_pattern  not in wordPatterns.allPatterns:
            continue # word not in dictionary, so carry on

        # Add letters of each candidate to mapping
        for candidate in wordPatterns.allPatterns[word_pattern]:
            addLettersToMapping(candidateMap, cipherword, candidate)

        # Intersect the new mapping with existing intersected mapping
        intersectedMap = intersectMappings(intersectedMap, candidateMap)

    # Remove any solved letters from the other lists
    return removeSolvedLetters(intersectedMap)
Esempio n. 14
0
def hackSimpleSub(message):
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    #sub()正则方法会用第一个参数替换第二个参数里任何匹配这个字符串模式的地方
    #cipherwordList中储存了message里每个单词的大写形式
    for cipherword in cipherwordList:
        # 给每一个密字建立一个密字映射
        newMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue  # 如果单词不在我们的字典里就跳过

        # 用候选词里的字母更新newMap里的密字映射
        for candidate in wordPatterns.allPatterns[wordPattern]:
            newMap = addLettersToMapping(newMap, cipherword, candidate)

        # 将新的密字映射与旧的交集取交集
        intersectedMap = intersectMappings(intersectedMap, newMap)

    # 从其他列表中移除已经被解密的字母
    return removeSolvedLettersFromMapping(intersectedMap)
def hackSimpleSub(message):
    #open the dictionary file and save it into a list so we only have to open it once
    dictionaryFile = 'dictionary.txt'
    dictionaryList = []
    with open(dictionaryFile, 'r') as f:
        for i in f:
            # Strip all '\n' characters from each of the dictionary words
            dictionaryList.append(i.strip('\n'))

    #<---Start of textbook code --->
    intersectedMap = getBlankCipherletterMapping()
    cipherwordList = nonLettersOrSpacePattern.sub('', message.upper()).split()
    for cipherword in cipherwordList:
        # Get a new cipherletter mapping for each ciphertext word:
        candidateMap = getBlankCipherletterMapping()

        wordPattern = makeWordPatterns.getWordPattern(cipherword)
        if wordPattern not in wordPatterns.allPatterns:
            continue  # This word was not in our dictionary, so continue.

        # Add the letters of each candidate to the mapping:
        for candidate in wordPatterns.allPatterns[wordPattern]:
            addLettersToMapping(candidateMap, cipherword, candidate)

        # Intersect the new mapping with the existing intersected mapping:
        intersectedMap = intersectMappings(intersectedMap, candidateMap)

    # Remove any solved letters from the other lists:
    firstMapping = removeSolvedLettersFromMapping(intersectedMap)
    underscoredText = decryptWithCipherletterMapping(message, firstMapping)
    #<---End of textbook code--->

    #<-------------- Start of my code ---------->
    # Turn the original text and the underscored text produced from the first mapping into two lists
    underscoredTextList = underscoredText.split(' ')
    originalMessageList = message.split(' ')

    # allPossibleLetters is a list of dictionaries that will be populated from the possible letter mappings
    # That are produced below
    allPossibleLetters = []
    for wordIndex in range(len(underscoredTextList)):
        # Get a word from the text and the matching cipher word
        word = underscoredTextList[wordIndex]
        cipherWord = originalMessageList[wordIndex]

        #if there's a missing character in the word we continue
        if '_' in word:

            # Strip all punctuation that would affect the dictionary search
            punctuation = ['.', ',', '!', '?', ')', '(', '*', '-', "'", '"']
            for i in punctuation:
                word = word.strip(i)
                cipherWord = cipherWord.strip(i)
            regularexpression = word

            # Here we create the regex token
            # If the word has an s at the end, search the dictionary for the word with and without an S
            # I assume that s denotes plural and plural words will not be in the dictionary
            if regularexpression[-1].lower() == 's':
                regularexpression = regularexpression + '?'
            # Replace _ with . to search for anything in that place
            regularexpression = '^' + regularexpression.upper().replace(
                '_', '.') + '$'

            # call checkWord function, returns a list of possible dictionary matches
            foundWords = checkWord(regularexpression, dictionaryList)

            # Create a dictionary for possible letter matches, keys are the cipherWord letter
            # value is a list of possible letters to replace that cipherLetter
            possibleLetters = {}
            for i in range(len(word)):
                if word[i] == '_':
                    # Find the corresponding letter in the possible word[s] for a missing letter
                    possibleLetters[cipherWord[i].upper()] = []
                    for possibleWord in foundWords:
                        possibleLetters[cipherWord[i].upper()].append(
                            possibleWord[i])

            allPossibleLetters.append(possibleLetters)

    # here we update the initial mapping
    # for each set of possible letters, this is per word that contained a _ after the initial mapping
    for letterSet in allPossibleLetters:
        # For each key in the letter set, check if it is in the initial mapping, if not remove it from the set
        for key in letterSet.keys():
            for possibleLetter in letterSet[key]:
                if possibleLetter not in firstMapping[key]:
                    letterSet[key].remove(possibleLetter)

            # Assuming at this point, we should be left with one to one pairs in the letterSet
            if len(letterSet[key]) == 1:
                # update the mapping to those one to one pairs
                # This is only done if the first mapping is not already a one to one pair (not really needed but an additional check)
                if letterSet[key][0] in firstMapping[key]:
                    if len(firstMapping[key]) > 1:
                        firstMapping[key] = letterSet[key]
    # remove solved letters from mapping to get rid of any mappings that still might contain multiple letters (again this is a precaution)
    secondMapping = removeSolvedLettersFromMapping(firstMapping)
    # return the complete message after decoding the ciphertext with the updated mapping
    completedMessage = decryptWithCipherletterMapping(message, secondMapping)

    return completedMessage
Esempio n. 16
0
word1 = 'OLQIHXIRCKGNZ'
word2 = 'PLQRZKBZB'
word3 = 'MPBKSSIPLC'

words = ['OLQIHXIRCKGNZ', 'PLQRZKBZB', 'MPBKSSIPLC']
letterMap1 = SimpleSubHacker.getBlackCipherLetterMapping()
letterMap2 = SimpleSubHacker.getBlackCipherLetterMapping()
letterMap3 = SimpleSubHacker.getBlackCipherLetterMapping()

letterMaps = []
letterMaps.append(letterMap1)
letterMaps.append(letterMap2)
letterMaps.append(letterMap3)

wordPat1 = makeWordPatterns.getWordPattern(word1)
wordPat2 = makeWordPatterns.getWordPattern(word2)
wordPat3 = makeWordPatterns.getWordPattern(word3)

patterns = []
patterns.append(wordPat1)
patterns.append(wordPat2)
patterns.append(wordPat3)
print(patterns)

cands = []
for pattern in patterns:
    cands.append(wordPatterns.allPatterns[pattern])

print(cands)
for candidateList in cands: