예제 #1
0
def d_substitution(ciphertext, key):
    plaintext = ""
    alphabet = utilities.adjust_key(utilities.get_baseString())
    #key = key

    #alphabet = utilities.adjust_key(alphabet)
    #print(alphabet)
    #print(key)
    for cipherChar in ciphertext:
        if (cipherChar.isupper()):
            upperFlag = True
        else:
            upperFlag = False
        cipherChar = cipherChar.lower()
        if cipherChar in alphabet:
            indx = key.index(cipherChar)
            plainChar = alphabet[indx]
            plainChar = plainChar.upper() if upperFlag else plainChar
        else:
            if (upperFlag == True):
                plainChar = cipherChar.upper()
            else:
                plainChar = cipherChar
        plaintext += plainChar
    #print(plaintext)
    #plaintext = utilities.adjust_key(plaintext)
    #print(utilities.adjust_key(plaintext))
    return plaintext
예제 #2
0
def e_substitution(plaintext, key):
    # your code here
    subString = key
    subString = utilities.adjust_key(subString)
    baseString = utilities.get_baseString()
    baseString = utilities.adjust_key(baseString)
    ciphertext = ''
    for plainChar in plaintext:
        #if(plainChar == 'Ã'):
        #print(plainChar.isupper())
        if (plainChar.isupper()):
            upperFlag = True
        else:
            upperFlag = False
        plainChar = plainChar.lower()
        if plainChar in baseString:
            indx = baseString.index(plainChar)
            cipherChar = subString[indx]
            cipherChar = cipherChar.upper() if upperFlag else cipherChar
        else:
            if (upperFlag == True):
                cipherChar = plainChar.upper()
            else:
                cipherChar = plainChar
        ciphertext += cipherChar
    #print(ciphertext)
    ciphertext = utilities.adjust_key(ciphertext)
    return ciphertext
예제 #3
0
def d_substitution(ciphertext,key):
    # your code here
    plaintext = ''
    alphabet = utilities.adjust_key(utilities.get_baseString())


    for cipherChar in ciphertext:
        upperFlag = True if cipherChar.isupper() else False
        cipherChar = cipherChar.lower()
        if cipherChar in alphabet:
            indx = key.index(cipherChar)
            plainChar = alphabet[indx]
            plainChar = plainChar.upper() if upperFlag else plainChar
        else:
            plainChar = cipherChar.upper() if upperFlag else cipherChar
        plaintext+= plainChar
    
    return plaintext
예제 #4
0
def e_substitution(plaintext,key):

    baseString = utilities.adjust_key(utilities.get_baseString())
    subString  = utilities.adjust_key(key)
    ciphertext = ''
    for plainChar in plaintext:
        upperFlag = True if plainChar.isupper() else False
        plainChar = plainChar.lower()
        if plainChar in baseString:
            indx = baseString.index(plainChar)
            cipherChar = subString[indx]
            cipherChar = cipherChar.upper() if upperFlag else cipherChar
        else:
             cipherChar = plainChar.upper() if upperFlag else plainChar
        ciphertext += cipherChar
    # if('#' in ciphertext):
    ciphertext = utilities.adjust_key(ciphertext)

    return ciphertext
예제 #5
0
def cryptanalysis_mathCipher(ciphertext):
    # your code here
    baseString = utilities.get_baseString()
    length = len(baseString)
    dictList = utilities.load_dictionary('engmix.txt')

    sub_baseString = []
    for j in range(25, length):
        sub_baseString.append(baseString[:j + 1])

    attempts = 0
    for n_s in sub_baseString:
        m = len(n_s)
        m_i_table = mod.mul_inv_table(m)
        for mi1 in m_i_table[0]:
            if m_i_table[1][mi1] != 'NA':
                for mi2 in m_i_table[0]:
                    if m_i_table[1][mi2] != 'NA':
                        for c in range(m):
                            if mod.residue(mi2**2 - c, m) != 0:
                                k = [mi1, mi2, c]
                                key = (n_s, k)
                                plaintext = d_mathCipher(ciphertext, key)
                                attempts += 1

                                if len(utilities.remove_nonalpha(
                                        plaintext)) < len(plaintext) / 2:
                                    continue

                                if utilities.is_plaintext(
                                        plaintext, dictList, 0.90):
                                    print('key found after ' + str(attempts) +
                                          ' attempts')
                                    return plaintext, key

    return '', ''
예제 #6
0
파일: solution.py 프로젝트: PJYGit/MT
def d_substitution(ciphertext, key):
    # your code here
    plaintext = ''
    ciphertext = ciphertext.replace('\n', '#')
    key = utilities.adjust_key(key)
    baseString = utilities.get_baseString()

    for cipherChar in ciphertext:
        upperFlag = True if cipherChar.isupper() else False
        cipherChar = cipherChar.lower()

        if cipherChar in key:
            Index = key.index(cipherChar)
            plainChar = baseString[Index]
            plainChar = plainChar.upper() if upperFlag else plainChar

        else:
            plainChar = cipherChar.upper() if upperFlag else cipherChar

        plaintext += plainChar

    plaintext = plaintext.replace('#', '\n')

    return plaintext
예제 #7
0
파일: solution.py 프로젝트: PJYGit/MT
def e_substitution(plaintext, key):
    # your code here
    ciphertext = ''
    plaintext = plaintext.replace('\n', '#')
    key = utilities.adjust_key(key)
    baseString = utilities.get_baseString()

    for plainChar in plaintext:
        upperFlag = True if plainChar.isupper() else False
        plainChar = plainChar.lower()

        if plainChar in baseString:
            Index = baseString.index(plainChar)
            cipherChar = key[Index]
            cipherChar = cipherChar.upper() if upperFlag else cipherChar

        else:
            cipherChar = plainChar.upper() if upperFlag else plainChar

        ciphertext += cipherChar

    ciphertext = ciphertext.replace('#', '\n')

    return ciphertext
예제 #8
0
파일: test_final.py 프로젝트: PJYGit/FT
def test_q1():
    print("-------------------------------------------")
    print("Testing Q1: MathCipher")
    print()

    alphabet = utilities.get_lower()

    print('Testing validity of keys:')
    keys = [['ab', [1, 1, 1]], ('', [1, 1, 1]), ('a', [1, 1, 1]),
            (123, [1, 1, 1]), ('ab', (1, 1, 1)), ('ab', [1, 1]),
            ('ab', [1, '1', 1]), (alphabet, [6, 7, 11]),
            (alphabet, [11, 13, 19]), (alphabet, [11, 7, 13])]
    for key in keys:
        print(key, ' = ', final.isValidKey_mathCipher(key))
    print()

    print('Testing Encryption/Decryption:')
    baseStr = alphabet
    k = [5, 9, 11]
    plaintext = 'MATH CIPHER'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = final.d_mathCipher(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!'
    k = [45, 5, 16]
    plaintext = 'Is mission accomplished?'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = final.d_mathCipher(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!#'
    k = [5, 5, 5]
    plaintext = ''
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print()
    plaintext2 = final.d_mathCipher(ciphertext, key)
    print()
    print()

    k = [421, 421, 421]
    baseStr = alphabet + '?!'
    key = (baseStr, k)
    plaintext = 'The quick brown fox jumps over the lazy dog'
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = final.d_mathCipher(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    k = [10, 3, 9]
    baseStr = alphabet + '#?!'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = final.d_mathCipher(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    k = [0, 1, 1]
    plaintext = 'This is Q1 of CP460 Final Exam'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = final.e_mathCipher(plaintext, key)
    print()
    plaintext2 = final.d_mathCipher(plaintext, key)
    print()
    print()

    print('Testing analyze_mathCipher:')
    base = 'ab'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = 'abc'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = 'abcd'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = 'abcde'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = 'abcdefg'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = utilities.get_lower()
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    base = base + '!#?'
    print('Analyze: ', base, '= ', final.analyze_mathCipher(base))
    print()

    print('Testing stats_mathCipher:')
    final.stats_mathCipher()
    print()

    print('Testing cryptanalysis:')
    plaintext = 'There are two kinds of cryptography in this world: cryptography that will stop your kid sister '
    plaintext += 'from reading your files, and cryptography that will stop major governments from reading your files.'
    baseString = utilities.get_baseString()[:26]
    key = (baseString, [3, 9, 11])
    ciphertext = final.e_mathCipher(plaintext, key)
    plaintext, key = final.cryptanalysis_mathCipher(ciphertext)
    if plaintext != '':
        print('Key = ', key)
        print('plaintext = ', plaintext)
    print()

    ciphertext = 'Uxcnoicostplqpcampcaptaugec ktptcxolcamutcdiptauog,cnoicyurrcxughctozpcmugat'
    print('ciphertext: ', ciphertext)
    plaintext, key = final.cryptanalysis_mathCipher(ciphertext)
    if plaintext != '':
        print('Key = ', key)
        print('plaintext = ', plaintext)
    print()

    print("-------------------------------------------")

    return