示例#1
0
def hackSimpleSubIsEnglish(message):

    tryNum = 1

    while 1:
        # Python programs can be stopped at any time by pressing Ctrl-C (on Windows) or Ctrl-D (on Mac and Linux)
        # brute-force by looping through every possible key
        decKey = getRandomKey()
        print('[%d th try] DecKey: %s' % (tryNum, decKey))
        decryptedText = simpleSubCipher.decryptMessage(decKey, message)

        if detectEnglish.isEnglish(decryptedText) > 0.20:
            # Check with the user to see if the decrypted key has been found.
            print()
            print('Possible Ciphertext hack:')
            print('Key: ' + str(decKey))
            print('Decrypted message: ' + decryptedText[:100])
            print()
            print('Enter D for done, or just press Enter to continue hacking:')
            response = input('> ')

            if response.upper().startswith('D'):
                return decryptedText

        tryNum += 1
        if tryNum >= ITERATION:
            return None
    return None
def main():
    message = """Sy l nlx sr pyyacao l ylwj eiswi upar lulsxrj isr
sxrjsxwjr, ia esmm rwctjsxsza sj wmpramh, lxo txmarr jia aqsoaxwa sr
pqaceiamnsxu, ia esmm caytra jp famsaqa sj. Sy, px jia pjiac ilxo, ia sr
pyyacao rpnajisxu eiswi lyypcor l calrpx ypc lwjsxu sx lwwpcolxwa jp isr
sxrjsxwjr, ia esmm lwwabj sj aqax px jia rmsuijarj aqsoaxwa. Jia pcsusx py
nhjir sr agbmlsxao sx jisr elh. -Facjclxo Ctrramm"""
    # message = "OLQIHXIRCKGNZ PLQRZKBZB MPBKSSIPLC"
#     message = """S ln lmcaloh ylc xpcji py Mpxopx, lxo lr S elmk sx jia rjcaajr py
# Bajacrftcui, S yaam l wpmo xpcjiacx fcaaza bmlh tbpx nh wiaakr, eiswi
# fclwar nh xacqar lxo ysmmr na esji oamsuij.  Op hpt txoacrjlxo jisr
# yaamsxu?  Jisr fcaaza, eiswi ilr jclqammao ycpn jia causpxr jpelcor
# eiswi S ln loqlxwsxu, usqar na l ypcajlrja py jipra swh wmsnar.
# Sxrbscsjao fh jisr esxo py bcpnsra, nh olhocalnr fawpna npca yacqaxj
# lxo qsqso.  S jch sx qlsx jp fa bacrtloao jilj jia bpma sr jia ralj py
# ycprj lxo oarpmljspx; sj aqac bcaraxjr sjramy jp nh snlusxljspx lr jia
# causpx py faltjh lxo oamsuij.  Jiaca, Nlculcaj, jia rtx sr ypcaqac
# qsrsfma, sjr fcplo osrk vtrj rkscjsxu jia ipcszpx lxo osyytrsxu l
# bacbajtlm rbmaxoptc.  Jiaca--ypc esji hptc malqa, nh rsrjac, S esmm btj
# rpna jctrj sx bcawaosxu xlqsuljpcr--jiaca rxpe lxo ycprj lca flxsriao;
# lxo, rlsmsxu pqac l wlmn ral, ea nlh fa elyjao jp l mlxo rtcblrrsxu sx
# epxoacr lxo sx faltjh aqach causpx isjiacjp osrwpqacao px jia ilfsjlfma
# umpfa.  Sjr bcpotwjspxr lxo yaljtcar nlh fa esjiptj aglnbma, lr jia
# biaxpnaxl py jia ialqaxmh fposar txoptfjaomh lca sx jipra txosrwpqacao
# rpmsjtoar.  Eilj nlh xpj fa agbawjao sx l wptxjch py ajacxlm msuij?  S
# nlh jiaca osrwpqac jia epxocptr bpeac eiswi ljjclwjr jia xaaoma lxo nlh
# cautmlja l jiptrlxo wamarjslm pfracqljspxr jilj cadtsca pxmh jisr
# qphlua jp caxoac jiasc raansxu awwaxjcswsjsar wpxrsrjaxj ypcaqac.  S
# rilmm rljslja nh lcoaxj wtcsprsjh esji jia rsuij py l blcj py jia epcmo
# xaqac faypca qsrsjao, lxo nlh jcalo l mlxo xaqac faypca snbcsxjao fh
# jia yppj py nlx. Jiara lca nh axjswanaxjr, lxo jiah lca rtyyswsaxj jp
# wpxdtac lmm yalc py olxuac pc oalji lxo jp sxotwa na jp wpnnaxwa jisr
# mlfpcsptr qphlua esji jia vph l wismo yaamr eiax ia anflckr sx l msjjma
# fplj, esji isr ipmsolh nljar, px lx agbaosjspx py osrwpqach tb isr
# xljsqa csqac. Ftj rtbbprsxu lmm jiara wpxvawjtcar jp fa ylmra, hpt
# wlxxpj wpxjarj jia sxarjsnlfma faxaysj eiswi S rilmm wpxyac px lmm
# nlxksxo, jp jia mlrj uaxacljspx, fh osrwpqacsxu l blrrlua xalc jia bpma
# jp jipra wptxjcsar, jp calwi eiswi lj bcaraxj rp nlxh npxjir lca
# cadtsrsja; pc fh lrwacjlsxsxu jia rawcaj py jia nluxaj, eiswi, sy lj
# lmm bprrsfma, wlx pxmh fa ayyawjao fh lx txoacjlksxu rtwi lr nsxa."""
    mapping = hack(message)
    if os.path.exists('sub_cipher_key_mapping.txt'):
        print('\'sub_cipher_key_mapping.txt\' already exists, overwite? (y/n)')
        response = input('>>')
        if not response.lower().startswith('y'):
            sys.exit('Abort execution')
    fo = open('sub_cipher_key_mapping.txt', 'w')
    fo.write(pprint.pformat(mapping))
    fo.close

    print()
    print('Message:')
    print(message)
    print()
    key = gen_key(mapping)
    print('Key: %s' % (key))
    translated = simpleSubCipher.decryptMessage(key, message)
    print('Translated:')
    print(translated)
示例#3
0
def decryptWithCipherletterMapping(ciphertext, letterMapping):
    key = ['x'] * len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            keyIndex = LETTERS.find(letterMapping[cipherletter][0])
            key[keyIndex] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(), '_')
            ciphertext = ciphertext.replace(cipherletter.upper(), '_')
    key = ''.join(key)
    return simpleSubCipher.decryptMessage(key, ciphertext)
def decryptWithCipherletterMapping(ciphertext, letterMapping):
    key = ['x']*len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            keyIndex = LETTERS.find(letterMapping[cipherletter][0])
            key[keyIndex] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(),'_')
            ciphertext = ciphertext.replace(cipherletter.upper(),'_')
    key = ''.join(key)
    return simpleSubCipher.decryptMessage(key,ciphertext)
示例#5
0
def decryptWithcipherLetterMapping(cipherText, letterMapping):
    key = ['x'] * len(LETTERS)
    for cipherLetter in LETTERS:
        if len(letterMapping[cipherLetter]) == 1:
            # If there's only one letter, add it to the key.
            keyIndex = LETTERS.find(letterMapping[cipherLetter][0])
            key[keyIndex] = cipherLetter
        else:
            cipherText = cipherText.replace(cipherLetter.lower(), '_')
            cipherText = cipherText.replace(cipherLetter.upper(), '_')
    key = ''.join(key)

    decryptedMessage = simpleSubCipher.decryptMessage(key, cipherText)

    return decryptedMessage
示例#6
0
def decryptWithCipherletterMapping(ciphertext, letterMapping):
    # Return a string of the ciphertext decrypted with the letter mapping,
    # with any ambiguous decrypted letters replaced with an underscore.

    key = ['x'] * len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            # If there's only one letter, add it to the key:
            keyIndex = LETTERS.find(letterMapping[cipherletter][0])
            key[keyIndex] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(), '_')
            ciphertext = ciphertext.replace(cipherletter.upper(), '_')
    key = "".join(key)

    # With the key we've created, decrypt the ciphertext:
    return simpleSubCipher.decryptMessage(key, ciphertext)
示例#7
0
def decryptWithCipherletterMapping(ciphertext, letterMapping):
    # 返回一个字符串作为密钥,里面包括已经被解密的密字,未解密的密字用下划线替代

    # First create a simple sub key from the letterMapping mapping.
    key = ['x'] * len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            # If there's only one letter, add it to the key.
            keyIndex = LETTERS.find(letterMapping[cipherletter][0])
            key[keyIndex] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(), '_')
            ciphertext = ciphertext.replace(cipherletter.upper(), '_')
    key = ''.join(key)

    # With the key we've created, decrypt the ciphertext.
    return simpleSubCipher.decryptMessage(key, ciphertext)
示例#8
0
def decryptWithCipherletterMapping(ciphertext, letterMapping):
    # Return a string of the ciphertext decrypted with the letter mapping,
    # with any ambiguous decrypted letters replaced with an _ underscore.

    # First create a simple sub key from the letterMapping mapping.
    key = ['x'] * len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            # If there's only one letter, add it to the key.
            keyIndex = LETTERS.find(letterMapping[cipherletter][0])
            key[keyIndex] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(), '_')
            ciphertext = ciphertext.replace(cipherletter.upper(), '_')
    key = ''.join(key)

    # With the key we've created, decrypt the ciphertext.
    return simpleSubCipher.decryptMessage(key, ciphertext)
def decryptWithMap(ciphertext, theMap):
    # This function will do a simple sub decryption of ciphertext with the
    # information in theMap, instead of a simple sub key.

    # First create a simple sub key from the theMap mapping.
    key = ['x'] * len(LETTERS)
    for letter in theMap.keys():
        if len(theMap[letter]) == 1:
            # If only one possible letter mapping, add it to the key.
            keyIndex = LETTERS.find(theMap[letter][0].upper())
            key[keyIndex] = letter.upper()
        else:
            ciphertext = ciphertext.replace(letter, '_')
    key = ''.join(key)

    # Then decrypt the original ciphertext with this key and return the
    # decryption.
    return simpleSubCipher.decryptMessage(key, ciphertext)
示例#10
0
def decryptWithCipherLetterMapping(ciphertext, letterMapping):
    # Return string of ciphertext decrypted with letter mapping
    # with any ambiguous decrypted letters replaced with  an underscore

    # Create simple sub key from letterMapping
    key = ['x'] * len(LETTERS)
    for cipherletter in LETTERS:
        if len(letterMapping[cipherletter]) == 1:
            # If only one letter add it to key
            key_index = LETTERS.find(letterMapping[cipherletter][0])
            key[key_index] = cipherletter
        else:
            ciphertext = ciphertext.replace(cipherletter.lower(), '_')
            ciphertext = ciphertext.replace(cipherletter.upper(), '_')
    key = ''.join(key)

    # With key created, decrypt ciphertext
    return simpleSubCipher.decryptMessage(key, ciphertext)
示例#11
0
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()
def simpleSubIsEnglishHacker(eMessage):
    tryNum = 1
    while tryNum <= ITER:
        #myKey = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
        decKey = simpleSubCipher.getRandomKey()
        #myMode = 'encrypt' # set to 'encrypt' or 'decrypt'

        plainText = simpleSubCipher.decryptMessage(decKey, eMessage)
        print()
        print('Possible encryption hack:')
        print('Key: ' + str(decKey))
        print('Decrypted message: ' + plainText[:100])

        if (detectEnglish.isEnglish(plainText)):
            print()
            print('Enter D for done, or just press Enter to continue hacking:')
            response = input('> ')

            if response.upper().startswith('D'):
                return plainText

        tryNum += 1

    return None
示例#13
0
def decryptMessage(key, message):
    key = makeSimpleSubKey(key)
    return simpleSubCipher.decryptMessage(key, message)
    decryptedList.append(z[1])
    print(symbolSet)
    print(''.join(decryptedList))

    # translated2 = simpleSubCipher.decryptMessage(''.join(frequancyTable), translated)
    # letterMapping = simpleSubHacker.hackSimpleSub(translated2)
    # translatedDecryption = simpleSubHacker.decryptWithCipherletterMapping(translated, letterMapping)
    # letterList = list(symbolSet)
    # decryptionKey = []

    # for i in range(0, len(symbolSet)):
    #     indexedEncrypt = letterList.index(encryptCharFreq[i].upper())
    #     decryptionKey.append(frequancyTable[indexedEncrypt])

    # print(symbolSet)
    # decryptedKey = ''.join(decryptionKey)
    # print(encryptCharFreq)

    translated = simpleSubCipher.decryptMessage(''.join(decryptedList),
                                                masterStringOriginal)
    letterMapping = simpleSubHacker.hackSimpleSub(translated)
    translatedDecryption = simpleSubHacker.decryptWithCipherletterMapping(
        translated, letterMapping)

    file1 = open(outputFile, "w")

    file1.write(translatedDecryption)
    file1.close()

    # print(encryptedCharacterFrequencymap.index('A'))
示例#15
0
def decryptMessage(key, message):
    key = makeSimpleSubKey(key)
    return simpleSubCipher.decryptMessage(key, message)
示例#16
0
                key = simpleSubCipher.getRandomKey()
                print("Your key is:\n>> ", key)
                to_continue()
                print(simpleSubCipher.encryptMessage(key, plain_text))
                to_continue()
                valid = False
            else:
                print("Please make a valid selection.")

    elif user_input in ['Substitution, Decrypt', 'Sd', '11', '11.']:
        cipher_text = input(cipher_text_decrypt)
        valid = True
        while valid:
            key = input("Enter encryption key:\n>> ").strip().upper()
            if simpleSubCipher.keyIsValid(key):
                print(simpleSubCipher.decryptMessage(key, cipher_text))
                to_continue()
                valid = False
            else:
                print(alpha_key_error)

    elif user_input in ['Substitution, Hack', 'Sh', '12', '12.']:
        cipher_text = input(cipher_text_hack)
        letter_mapping = simpleSubHacker.hackSimpleSub(cipher_text)
        print("\nLetter Mapping:\n", letter_mapping)
        print("\nOriginal Ciphertext:\n", cipher_text)
        hacked, key = simpleSubHacker.decryptWithCipherletterMapping(cipher_text, letter_mapping)
        print("\nHacked Message:\n", hacked)
        print("\nProposed Key: ", key)
        to_continue()
        #need to add a loop/option here where I can go directly to the sub-cipher decrypt and try the