Esempio n. 1
0
def task6():
    filename = 'images/im8_caesar_ofb_c_all.bmp'
    key = 56
    iv = 9
    cipher_data = read(filename)
    decrypted = ofb_decrypt(cipher_data, iv, key, type='caesar')
    write('decrypted_images/task6.bmp', decrypted)
    # ofb encryption
    data = read('decrypted_images/task6.bmp')
    encrypted = ofb_encrypt(data, iv, key, type='caesar', thr=50)
    write('decrypted_images/task6_ofb_50.bmp', encrypted)
    # # ecb encryption
    encrypted = caesar.encrypt_data(data, key, thr=50)
    write('decrypted_images/task6_ecb_50.bmp', encrypted)
Esempio n. 2
0
def task8():
    filename = 'images/z3_caesar_ctr_c_all.bmp'
    cipher_data = read(filename)
    key = 223
    iv = 78
    decrypted = ctr_decrypt(cipher_data, iv, key, 'caesar')
    write('decrypted_images/task8.bmp', decrypted)
    # encr ctr
    data = read('decrypted_images/task8.bmp')
    encrypted = ctr_encrypt(data, iv, key, 'caesar', thr=50)
    write('decrypted_images/task8_ctr_50.bmp', encrypted)
    # encr ecb
    encrypted = caesar.encrypt_data(data, key, thr=50)
    write('decrypted_images/task8_ecb_50.bmp', encrypted)
Esempio n. 3
0
def task5():
    filename = 'images/z1_caesar_cbc_c_all.bmp'
    encrypted_data = read(filename)
    key = 223
    iv = 59
    decrypted = cbc_decrypt(encrypted_data, iv, key, type='caesar')
    write('decrypted_images/task5.bmp', decrypted)

    # encryption of task5.bmp using CBC except first 50 bytes
    data = read('decrypted_images/task5.bmp')
    encrypted = cbc_encrypt(data, iv, key, type='caesar', thr=50)
    write('decrypted_images/task5_cbs_50.bmp', encrypted)
    # encryption of task5.bmp using ECB except first 50 bytes
    encrypted = caesar.encrypt_data(data, key, thr=50)
    write('decrypted_images/task5_ecb_50.bmp', encrypted)
Esempio n. 4
0
def task17():
    filename = 'images/z4_affine_сtr_c_all.bmp'
    cipher_data = read(filename)
    key = {'a': 61, 'b': 18}
    iv = 92
    decrypted = ctr_decrypt(cipher_data, iv, key, 'affine')
    write('decrypted_images/task17.bmp', decrypted)
Esempio n. 5
0
def task16():
    filename = 'images/im17_affine_сfb_c_all.bmp'
    cipher_data = read(filename)
    key = {'a': 117, 'b': 239}
    iv = 19
    decrypted = cfb_decrypt(cipher_data, iv, key, type='affine')
    write('decrypted_images/task16.bmp', decrypted)
Esempio n. 6
0
def task15():
    filename = 'images/im16_affine_ofb_c_all.bmp'
    cipher_data = read(filename)
    key = {'a': 233, 'b': 216}
    iv = 141
    decrypted = ofb_decrypt(cipher_data, iv, key, type='affine')
    write('decrypted_images/task15.bmp', decrypted)
Esempio n. 7
0
def task14():
    filename = 'images/im15_affine_cbc_c_all.bmp'
    cipher_data = read(filename)
    key = {'a': 129, 'b': 107}
    iv = 243
    decrypted = cbc_decrypt(cipher_data, iv, key, type='affine')
    write('decrypted_images/task14.bmp', decrypted)
Esempio n. 8
0
def task13():
    filename = 'images/z6_vigener_ctr_c_all.bmp'
    cipher_data = read(filename)
    key = [ord(ch) for ch in 'MONOLITH']
    iv = 167
    res = ctr_decrypt(cipher_data, iv, key, 'vigenere')
    write('decrypted_images/task13.bmp', res)
Esempio n. 9
0
def task12():
    filename = 'images/im5_vigener_cfb_c_all.bmp'
    cipher_data = read(filename)
    key = [ord(ch) for ch in 'MONARCH']
    iv = 172
    res = cfb_decrypt(cipher_data, iv, key, type='vigenere')
    write('decrypted_images/task12.bmp', res)
Esempio n. 10
0
def task7():
    filename = 'images/z2_caesar_cfb_c_all.bmp'
    cipher_data = read(filename)
    iv = 9
    key = 174
    decrypted = cfb_decrypt(cipher_data, iv, key, type='caesar')
    write('decrypted_images/task7.bmp', decrypted)
Esempio n. 11
0
def task12():
    encrypted_data = read('./text_files/text2_permutation_c.txt')
    for key in permutations([0, 1, 2, 3, 4, 5]):
        decrypted_text = decrypt(encrypted_data, key, len(key))
        if isEnglish(decrypted_text):
            print(f"Possible key: {key}")
            print(f'Message part: {decrypted_text}')
Esempio n. 12
0
def task5():
    a = 19
    b = 236
    a_inv = find_mod_inverse(a, 256)
    encrypted_bmp_data = read('./imgs/ff2_affine_c_all.bmp')
    decrypted_bmp_data = affine.decrypt_data(encrypted_bmp_data, a_inv, b)
    write('./imgs/task5_decrypted.BMP', decrypted_bmp_data)
Esempio n. 13
0
def task11():
    filename = 'images/im4_vigener_ofb_c_all.bmp'
    cipher_data = read(filename)
    key = [ord(ch) for ch in 'MODULATOR']
    iv = 217
    res = ofb_decrypt(cipher_data, iv, key, type='vigenere')
    write('decrypted_images/task11.bmp', res)
Esempio n. 14
0
def task10():
    filename = 'images/z5_vigener_cbc_c_all.bmp'
    cipher_data = read(filename)
    key = [ord(ch) for ch in 'MODELING']
    iv = 67
    decrypted = cbc_decrypt(cipher_data, iv, key, type='vigenere')
    write('decrypted_images/task10.bmp', decrypted)
Esempio n. 15
0
def task2():
    filename = 'images/b4_hill_c_all.png'
    encrypted_data = read(filename)
    png_bytes = np.array([[137, 78], [80, 71]])
    enc_bytes = np.array([[23, 239], [3, 52]])
    inv_png_bytes = find_inverse_key(png_bytes)
    key = np.matmul(enc_bytes, inv_png_bytes) % ALPHABET_SIZE
    decode(filename, 'decrypted_images/task2.png', key)
Esempio n. 16
0
def task11():
    encrypted_data = read('./text_files/text1_vigener_c.txt')
    # known_data = [ord(ch) for ch in 'it therefore']
    # keys = guess_keys(encrypted_data, known_data)

    key = 'KEYBOARD'
    decrypted_data = decrypt(encrypted_data, key)
    print(''.join(chr(ch) for ch in decrypted_data))
Esempio n. 17
0
def task2():
    encrypted_img_data = read('./imgs/c4_caesar_c_all.bmp')
    for key in range(256):
        decrypted = Caesar.decrypt_data(encrypted_img_data[:2], key)
        if decrypted == [66, 77]:
            print(f'Key is {key}')
            break
    decrypted_bmp = Caesar.decrypt_data(encrypted_img_data, key)
    write('./imgs/task2_decrypted_bmp.BMP', decrypted_bmp)
Esempio n. 18
0
def decode(filename, output_name, key):
    encrypted_data = read(filename)
    inv_key = find_inverse_key(key)
    decrypted_data = []
    for i in range(0, len(encrypted_data), 2):
        d = np.array([encrypted_data[i], encrypted_data[i+1]])
        decrypted = np.matmul(inv_key, d) % ALPHABET_SIZE
        decrypted_data += list(decrypted)
    write(output_name, data=decrypted_data)
Esempio n. 19
0
def task1():
    encrypted_data = read('./text_files/t3_caesar_c_all.txt')
    for key in range(256):
        decrypted = Caesar.decrypt_data(encrypted_data[:25], key)
        txt = ''.join(chr(ch) for ch in decrypted)
        if isEnglish(txt):
            break
    decrypted_data = Caesar.decrypt_data(encrypted_data, key)
    write('./text_files/task1_decrypted.txt', decrypted_data)
Esempio n. 20
0
def main():
    print('\nFor copy-paste convenience, \ndouble-click and copy:\n')
    show_PDFs_in_current_directory()

    file_name = file_prompt()

    page_texts = get_page_texts_in_pdf(file_name)

    write('backup.txt', '\n'.join(page_texts))
    txt_string = read('backup.txt')

    lines = txt_string.split('\n')

    hst_lines = []
    rebate_lines = []
    total_lines = []
    month = ''
    for index, line in enumerate(lines):
        month = get_month(lines, index, line)
        if month:
            hst_lines.append(month)
            rebate_lines.append(month)
            total_lines.append(month)

        hst_util = 'HST on Gas '
        hst_hydro = 'Harmonized Sales Tax on $'
        hst_hydro_new = 'HST ('
        if hst_util in line or hst_hydro in line or hst_hydro_new in line:
            # HST
            hst_lines.append(get_number_at_end_of_line(line))

        rebate_hydro = 'Electricity Rebate'
        if rebate_hydro in line:
            # rebate
            rebate_lines.append(get_credit_at_end_of_line(line))

        total_util_g_w = 'Total Gas & Water Charges'
        total_util_s = 'Total  Stormwater Rate Charges'
        if total_util_g_w in line:
            # total, including HST
            total_lines.append('GW ' + get_number_at_end_of_line(line))
        if total_util_s in line:
            # total, including HST
            total_lines.append(' S ' + get_number_at_end_of_line(line))

        total_hydro = 'PAYABLEONOR'  # always line above
        total_hydro_new = 'What Do I Owe?'  # always line above
        if total_hydro in line or total_hydro_new in line:
            # total, including HST
            next_line = lines[index + 1]
            total_lines.append(get_total_for_hydro_new(next_line))

    write('_hst.txt', '\n'.join(hst_lines))
    write('_rebate.txt', '\n'.join(rebate_lines))
    write('_total.txt', '\n'.join(total_lines))
Esempio n. 21
0
def task10():
    encrypted_data = read('./text_files/text4_vigener_c_all.txt')
    known_data = [ord(ch) for ch in 'housewives']
    for i in range(len(encrypted_data) - len(known_data)):
        text = ''
        for j in range(len(known_data)):
            ch = (encrypted_data[i + j] - known_data[j]) % alphabet_size
            text += chr(ch)
            if isEnglish(text):
                print(f'**** POSSIBLE ENGLISH: {text} ******')
        print(text)
Esempio n. 22
0
def task9():
    data = read('decrypted_images/task5.bmp')
    key = 223
    iv = 78
    encrypted_data_ecb = caesar.encrypt_data(data, key, thr=50)
    write('task9_ecb.bmp', encrypted_data_ecb)
    encrypted_data_ofb = ofb_encrypt(data, iv, key, type='caesar', thr=50)
    write('task9_ofb.bmp', encrypted_data_ofb)
    encrypted_data_cfb = cfb_encrypt(data, iv, key, type='caesar', thr=50)
    write('task9_cfb.bmp', encrypted_data_cfb)
    encrypted_data_cbc = cbc_encrypt(data, iv, key, type='caesar', thr=50)
    write('task9_cbc.bmp', encrypted_data_cbc)
    encrypted_data_ctr = ctr_encrypt(data, iv, key, type='caesar', thr=50)
    write('task9_ctr.bmp', encrypted_data_ctr)
Esempio n. 23
0
def task9():
    encrypted_data = read('./text_files/text4_vigener_c_all.txt')
    possible_word = ' ' * 10
    data = [ord(' ')] * len(possible_word)
    found_word = ''
    english_words = []
    for i in range(len(encrypted_data) - len(possible_word)):
        for j in range(len(possible_word)):
            ch = (encrypted_data[i + j] - data[j]) % alphabet_size
            found_word += chr(ch)
            if isEnglish(found_word):
                english_words.append(found_word)
        possible_key = longest_word(english_words)
        if possible_key:
            return possible_key
        found_word = ''
    return None
Esempio n. 24
0
def task7():
    encrypted_png_data = read('./imgs/b4_affine_c_all.png')
    count = 0
    keys = None
    for a in range(256):
        if keys:
            break
        for b in range(256):
            if gcd(a, 256) == 1:
                a_inv = find_mod_inverse(a, 256)
                if a_inv is None:
                    continue
                count += 1
                decrypted_data = affine.decrypt_data(encrypted_png_data[:2],
                                                     a_inv, b)
                if decrypted_data == [137, 80]:
                    print(f'Keys are: a={a}, b={b}, Attempts: {count}')
                    keys = [a, b]
    a, b = keys
    decrypted_png_data = affine.decrypt_data(encrypted_png_data,
                                             find_mod_inverse(a, 256), b)
    write('./imgs/task7_decrypted.png', decrypted_png_data)
Esempio n. 25
0
def task6():
    encrypted_data = read('./text_files/text10_affine_c_all.txt')
    count = 0
    keys = None
    for a in range(256):
        if keys:
            break
        for b in range(256):
            if gcd(a, 256) == 1:
                a_inv = find_mod_inverse(a, 256)
                if a_inv is None:
                    continue
                count += 1
                decrypted_data = affine.decrypt_data(encrypted_data, a_inv, b)
                txt = ''.join(chr(ch) for ch in decrypted_data)
                if isEnglish(txt):
                    print(f"Keys are: a={a}, b={b}\nAttempts: {count}")
                    keys = [a, b]
                    break
    a, b = keys
    decrypted_msg = affine.decrypt_data(encrypted_data,
                                        find_mod_inverse(a, 256), b)
    txt = ''.join(chr(ch) for ch in decrypted_msg)
    print(f'Original msg:\n{txt}')
Esempio n. 26
0
def task8():
    encrypted_data = read('./imgs/im6_vigener_c_all.bmp')
    decrypted_data = decrypt(encrypted_data, 'magistr')
    write('./imgs/task8_decrypted.bmp', decrypted_data)
Esempio n. 27
0
def read_file(filename):
    data = read(filename)
    return data
Esempio n. 28
0
def task8_plus():
    bmp_data = read('task8_decrypted.bmp')
    encrypted_data = encrypt(bmp_data, 'magistr', border=50)
    print(bmp_data[:50] == encrypted_data[:50])
    write('./imgs/task8_encr50.bmp', encrypted_data)
Esempio n. 29
0
def task9():
    encrypted_data = read('./text_files/text4_vigener_c_all.txt')
    key = 'student'
    decrypted = decrypt(encrypted_data, key)
    print(''.join(chr(ch) for ch in decrypted))
Esempio n. 30
0
def task3():
    encrypted_data = read('c3_subst_c_all.png')
    decrypted_data = substitution.decrypt(encrypted_data)
    write('./imgs/task3_decrypted.png', decrypted_data)