def main(countRun):

    fileName = "../main.resources/mixedText.txt"
    maxKeyLength = 24
    randomizer = 1
    randomizer_action = "add"
    maxMsgLength = 500
    testNum=2

    for _ in range (countRun):
        input_cipher=encryptor.cipherText(fileName, maxKeyLength, randomizer, randomizer_action, testNum, maxMsgLength)
        decryptor.arrayPopulator(fileName, testNum, input_cipher)
        print('\n')
        print("======================================================================")
        print("======================================================================")
        print('\n')
Exemple #2
0
def encryptor(fileName, maxKeyLength, randomizer, randomizer_action, testNum,
              maxMsgLength):

    ciphertext_str = cipherText(fileName, maxKeyLength, randomizer,
                                randomizer_action, testNum, maxMsgLength)

    tot_runtime = decryptor.arrayPopulator(fileName, testNum, ciphertext_str)

    # print(f"**************Runtime of the Decryptor is {tot_runtime}")

    return (tot_runtime)
Exemple #3
0
def encryptor(maxKeyLength, randomizer, randomizer_action, testNum,
              maxMsgLength):
    alphaDict = {
        0: ' ',
        1: 'a',
        2: 'b',
        3: 'c',
        4: 'd',
        5: 'e',
        6: 'f',
        7: 'g',
        8: 'h',
        9: 'i',
        10: 'j',
        11: 'k',
        12: 'l',
        13: 'm',
        14: 'n',
        15: 'o',
        16: 'p',
        17: 'q',
        18: 'r',
        19: 's',
        20: 't',
        21: 'u',
        22: 'v',
        23: 'w',
        24: 'x',
        25: 'y',
        26: 'z'
    }

    ciphertext = []
    ciphertext_flat = []
    ciphertext_str = ""
    encryptKey = []
    random_chars = 0

    # generate random key
    encryptKey = enc_key_gen(maxKeyLength)
    t = len(encryptKey)

    if testNum == 1:
        plaintext_from_file = \
            test1_plaintext_gen("../main.resources/test1dict.txt")
    else:
        plaintext_from_file = \
            test2_plaintext_gen("../main.resources/test2dict_400.txt", maxMsgLength)

    plaintext = plaintext_from_file
    plaintext_len = len(plaintext)
    s_i = m_i = 0

    while m_i < plaintext_len:
        if randomizer_action == "add":
            k_i = s_i % t + randomizer  # randomizer is static for the entire encryption run - it's passed in
            # k_i = (2*s_i + 3) % t + randomizer  # randomizer is static for the entire encryption run - it's passed in
        else:
            k_i = s_i % t - randomizer  # randomizer is static for the entire encryption run - it's passed in

        if k_i >= t or k_i < 0:
            s_i += 1
            randchar_k = randint(0, 26)
            ciphertext.insert(
                len(ciphertext),
                [v for k, v in alphaDict.items() if k == randchar_k])
            #print("Inserted random char: ", ciphertext[len(ciphertext)-1], "At index: ", len(ciphertext)-1)
            random_chars += 1
            continue
        else:
            plaintext_k = [
                k for k, v in alphaDict.items() if v == plaintext[m_i]
            ]
            ciphertext_k = plaintext_k[0] - encryptKey[k_i]
            if ciphertext_k < 0:
                ciphertext_k += len(alphaDict)
            ciphertext.insert(
                len(ciphertext),
                [v for k, v in alphaDict.items() if k == ciphertext_k])

        m_i += 1
        s_i += 1

    # print("Modified Plaintext (# indicates random char", plaintext)
    print("Plaintext Length:", len(plaintext_from_file),
          "  Ciphertext length:", len(ciphertext), "Rand Chars: ",
          random_chars)
    rand_percent = round((random_chars / len(plaintext)) * 100, 2)
    print("random chars in cipher text: ", rand_percent, "%")

    for elem in ciphertext:
        ciphertext_flat.extend(elem)
    ciphertext_str = ''.join(ciphertext_flat)

    # print("Cipher text list: ", ciphertext_flat)
    if testNum == 2:
        print("Randomly Generated Plaintext (from dict2): '%s'" % plaintext)
    print("Cipher text str: '%s'" % ciphertext_str)

    start = time.time()
    decryptor.arrayPopulator(testNum, ciphertext_str)
    end = time.time()

    print(f"**************Runtime of the program is {end - start}")