Exemple #1
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
Exemple #2
0
def test_q2(file1, file2):
    print('Testing Encryption/decryption against sample file:')
    plaintext = utilities.file_to_text('plaintext_subcipher_sample.txt')
    ciphertext = utilities.file_to_text('ciphertext_subcipher_sample.txt')
    key = utilities.adjust_key("""poejiqsrbltxwaznfcdhmvgkuy:'" !.?,-#;""")
    ciphertext2 = solution.e_substitution(plaintext, key)
    if ciphertext == ciphertext2:
        print('\tEncryption Successful')
    else:
        print('\tEncryption failed')
    plaintext2 = solution.d_substitution(ciphertext2, key)
    if plaintext == plaintext2:
        print('\tDecryption Successful')
    else:
        print('\tDecryption failed')
    print()

    # this is given you do not need to do anything for it
    print('Cryptanalysis for Sample File:')
    key = '''poejiqsrbltxwaznfcdhmvgkuy:'" !.?,-#;'''
    print('found key = ', key)
    print()

    # Cryptanalysis of your file
    plaintext = ''
    ciphertext = ''
    ciphertext2 = ''
    plain1 = file1
    cipher1 = file2
    key = 0
    ciphertext = utilities.file_to_text(cipher1)
    print('Cryptanalysis of {}:'.format(cipher1))
    key, plaintext = solution.cryptanalysis_substitution(ciphertext)
    print('Key found: ', utilities.adjust_key(key))
    print()
    print('Plaintext:')
    print(plaintext[:150])
    print()
    print('Verifying cryptanalysis results: ', end='')
    ciphertext2 = solution.e_substitution(plaintext, key)
    if ciphertext == ciphertext2:
        print('OK')
    else:
        print('Something is not right!')
    print()

    solution.comments_q2()
    return
Exemple #3
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
Exemple #4
0
def cryptanalysis_substitution(ciphertext):
    key = '''yhribekafszmpxwojductnvlgq!-#;". '?,:'''   #<--- change this line with your key
    key = utilities.adjust_key(key)                     #<--- keep this line
    plaintext =''
    # add lines to decrypt the ciphertext
    # remember to write your plaintext to file
    return key,plaintext
Exemple #5
0
def cryptanalysis_substitution(ciphertext):
    key = '''smzokelxrcitbudnhwfpyaqvjg':"!-; ,#?.'''  # <--- change this line with your key
    key = utilities.adjust_key(key)  # <--- keep this line
    # add lines to decrypt the ciphertext
    plaintext = d_substitution(ciphertext, key)
    # remember to write your plaintext to file
    utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q3.txt')
    return key, plaintext
Exemple #6
0
def cryptanalysis_substitution(ciphertext):
    #key ='''rlpdeogkvbayiucntwqhfjxsmz //,"'?;///'''
    key = '''rlpdeogkvbayiucntwqhfjxsmz ,.:"'?;!-#'''
    key = utilities.adjust_key(key)
    # add lines to decrypt the ciphertext
    # key is not totally correct, fix this
    plaintext = d_substitution(ciphertext, key)

    return key, plaintext
Exemple #7
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
Exemple #8
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
Exemple #9
0
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
Exemple #10
0
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