Esempio n. 1
0
def decrypt_message(ciphertext):
    """Decrypts the ciphertext."""

    ciphertext_binary = helper.convert_text_to_binary(ciphertext)
    ciphertext_binary = pad_with_zeros(ciphertext_binary)
    ciphertext_len = len(ciphertext_binary)
    plaintext_binary = []

    for i in range(0, ciphertext_len, 64):
        block = ciphertext_binary[i:i + 64]
        decrypted_block = decrypt_block(block)
        plaintext_binary.append(decrypted_block)

    plaintext_binary = ''.join(plaintext_binary)
    plaintext = helper.convert_binary_to_text(plaintext_binary)
    plaintext = remove_padding_chars(plaintext)
    return plaintext
Esempio n. 2
0
def encrypt_message(plaintext):
    """Encrypts the plaintext."""

    plaintext_binary = helper.convert_text_to_binary(plaintext)
    plaintext_binary = pad_with_zeros(plaintext_binary)
    plaintext_len = len(plaintext_binary)
    round_keys = get_round_keys()
    ciphertext_binary = []

    for i in range(0, plaintext_len, 64):
        block = plaintext_binary[i:i + 64]
        encrypted_block = encrypt_block(block, round_keys)
        ciphertext_binary.append(encrypted_block)

    ciphertext_binary = ''.join(ciphertext_binary)
    ciphertext = helper.convert_binary_to_text(ciphertext_binary)
    return ciphertext
Esempio n. 3
0
def encrypt_message(plaintext):
	"""Encrypts the plaintext."""

	plaintext_binary = convert_text_to_binary(plaintext)
	block_len = int(len(plaintext_binary)//2)
	left_portion = plaintext_binary[0:block_len]
	right_portion = plaintext_binary[block_len::]
	num_rounds = 2
	keys = get_keys()

	for i in range(num_rounds):
		right_portion_len = len(right_portion)
		random_key = keys['K{}'.format(i + 1)]
		temp = string_xor(right_portion, random_key)

		new_left_portion = right_portion
		right_portion = string_xor(temp, left_portion)
		left_portion = new_left_portion

	ciphertext = left_portion + right_portion
	return convert_binary_to_text(ciphertext)
Esempio n. 4
0
def decrypt_message(ciphertext):
    """Decrypts the ciphertext."""

    ciphertext_binary = convert_text_to_binary(ciphertext)
    block_len = int(len(ciphertext_binary) // 2)
    left_portion = ciphertext_binary[0:block_len]
    right_portion = ciphertext_binary[block_len::]
    num_rounds = 2
    keys = get_keys()
    num_keys = len(keys)

    for i in range(num_rounds):
        left_portion_len = len(left_portion)
        random_key = keys['K{}'.format(num_keys - i)]
        temp = string_xor(left_portion, random_key)

        new_right_portion = left_portion
        left_portion = string_xor(temp, right_portion)
        right_portion = new_right_portion

    plaintext = left_portion + right_portion
    return convert_binary_to_text(plaintext)
Esempio n. 5
0
    with open('diffie_hellman/keys.json') as f:
        keys = json.load(f)
    return keys


def decrypt_num(input_num):
    """Decrypts a number."""

    keys = get_keys()
    sender_public_key = keys['sender_public_key']
    private_key = keys['receiver_secret_key']
    prime_num = keys['prime_number']
    super_key = pow(sender_public_key, private_key, prime_num)
    decrypted_num = (input_num - super_key + 2 * prime_num) % prime_num
    return decrypted_num


if __name__ == '__main__':

    ciphertext = take_input()
    encrypted_binary = helper.convert_text_to_binary(ciphertext)
    encrypted_num = int(encrypted_binary, 2)
    decrypted_num = decrypt_num(encrypted_num)
    bit_len = decrypted_num.bit_length()
    if bit_len % 8 != 0:
        bit_len = (bit_len // 8 + 1) * 8
    decrypted_binary = format(decrypted_num, '0{}b'.format(bit_len))
    decrypted_message = helper.convert_binary_to_text(decrypted_binary)
    print("Decrypted message is {}".format(repr(decrypted_message)))