Esempio n. 1
0
def caesar():
    key = 1
    is_error = False

    for index, arg in enumerate(sys.argv):
        if arg in ['--key', '-k'] and len(sys.argv) > index + 1:
            key = int(sys.argv[index + 1])
            del sys.argv[index]
            del sys.argv[index]
            break

    for index, arg in enumerate(sys.argv):
        if arg in ['--encrypt', '-e']:
            del sys.argv[index]
            break
        if arg in ['--decrypt', '-d']:
            key = -key
            del sys.argv[index]
            break

    if len(sys.argv) == 1:
        is_error = True
    else:
        for arg in sys.argv:
            if arg.startswith('-'):
                is_error = True

    if is_error:
        print(
            f'Usage: python {sys.argv[0]} [ --key <key> ] [ --encrypt|decrypt ] <text>'
        )
    else:
        print(encrypt(' '.join(sys.argv[1:]), key))
Esempio n. 2
0
def crack_caesar(input_file, output_file):
    """This function reads encrypted text from an input file and iterates over the 25 possible
    caesar ciphers to determine the most likely decrypted plaintext message and caesar cipher
    key used to encrypt the text. For each caesar cipher, the number of valid english words are
    determined by checking against a dictionary provided by the `pyenchant` module. The most
    likely plaintext and key are determined by the caesar cipher containing the highest number of
    valid english words.
    """
    ciphertext = input_file.read()
    en_dict = enchant.Dict('en_US')
    max_num_valid_words = 0
    # tqdm module enables us to wrap ANY iterable with `tdqm(iterable` to turn our loops
    # into a smart progress bar!
    for key in tqdm(range(1, 26)):
        plaintext = encrypt(ciphertext, -key)
        num_valid_words = 0
        for word in plaintext.split(' '):
            if en_dict.check(word):
                num_valid_words += 1
        if num_valid_words > max_num_valid_words:
            max_num_valid_words = num_valid_words
            best_plaintext = plaintext
            best_key = key
    click.echo(f'The most likely encryption key is {best_key}')
    output_file.write(best_plaintext)
def caesar(text, decrypt, key):
    text_string = ' '.join(text)
    if decrypt:
        key = -key
    cyphertext = encrypt(text_string, key)
    # echo is same as print
    click.echo(cyphertext)
def caesar(input_file, output_file, decrypt, key):
    if input_file:
        text = input_file.read()
    else:
        text = click.prompt('Enter a text', hide_input=not decrypt)
    if decrypt:
        key = -key
    cyphertext = encrypt(text, key)
    if output_file:
        output_file.write(cyphertext)
    else:
        click.echo(cyphertext)
def caesar():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-e', '--encrypt', action='store_true')
    group.add_argument('-d', '--decrypt', action='store_true')
    parser.add_argument('text', nargs='*')
    parser.add_argument('-k', '--key', type=int, default=1)
    args = parser.parse_args()

    text_string = ' '.join(args.text)
    key = args.key
    if args.decrypt:
        key = -key
    cyphertext = encrypt(text_string, key)
    print(cyphertext)
def caesar_breaker(input_file, output_file):
    cyphertext = input_file.read()
    english_dictionnary = enchant.Dict("en_US")
    best_number_of_english_words = 0
    for key in tqdm(range(26)):
        plaintext = encrypt(cyphertext, -key)
        number_of_english_words = 0
        for word in plaintext.split(' '):
            if word and english_dictionnary.check(word):
                number_of_english_words += 1
        if number_of_english_words > best_number_of_english_words:
            best_number_of_english_words = number_of_english_words
            best_plaintext = plaintext
            best_key = key
    click.echo(f'The most likely encryption key is {best_key}. It gives the following plaintext:\n\n{best_plaintext[:1000]}...')
    output_file.write(best_plaintext)
def caesar(input_file, output_file, key, decrypt):
    """This function uses the click module to parse the
    arguments passed in from the command line and encrypts/decrypts
    the message using the `encrypt` function from the `caesar_encryption`
    module. Error and handling and usage (e.g. --help) is automatically
    generated for us.
    """
    if input_file:
        text = input_file.read()
    else:
        # if user wants to encrypt text, we don't want to have this show up in the command history
        text = click.prompt('Please enter a text', hide_input=not decrypt)

    if decrypt:
        key = -key

    ciphertext = encrypt(text, key)

    if output_file:
        output_file.write(ciphertext)
    else:
        click.echo(ciphertext)
Esempio n. 8
0
def main(plain_text, shift, key=None):
    encrypted_msg = caesar_encryption.encrypt(text, shift)
    decrypted_msg = caesar_decryption.decrypt(encrypted_msg, key)
    return encrypted_msg, decrypted_msg