diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index f836cc5..d257e2c 100644 --- a/src/algorithms/decrypt.py +++ b/src/algorithms/decrypt.py @@ -1,5 +1,5 @@ import base64 -from src.utils.binary import transform_char_to_ascii, divide_binary, xor_binary_values, transform_ascii_to_char +from src.utils.binary import transform_char_to_ascii, divide_binary, xor_binary_values, transform_ascii_to_char, shift_left from fastapi import HTTPException, status @@ -19,7 +19,7 @@ def decrypt_message(encrypted_message: str, key: str) -> str: binary_array = binary_array_string.replace("[", "").replace("]", "").replace("'", "").split(", ") key_ascii = [] - decrypted_in_binary_ascii = [] + binary_ascii = [] for char in reversed_key: key_ascii.append(transform_char_to_ascii(char)) @@ -28,20 +28,18 @@ def decrypt_message(encrypted_message: str, key: str) -> str: operator = binary for char_key in key_ascii: confussion_decrypt_result = _confussion_decrypt(operator, char_key, len(key)) - - # TODO: Call Difussion Decrypt here with the result of the confussion decrypt - # Then assign the result of the difussion to the operator - - operator = confussion_decrypt_result - - decrypted_in_binary_ascii.append(operator) + # diffusion_decrypt_result = _diffusion_decrypt_shift(confussion_decrypt_result, char_key) + operator = confussion_decrypt_result ## cambiar a diffusion_decrypt + + binary_ascii.append(operator) + + result_decrypt = _diffusion_decrypt_transposition(binary_ascii, key) decrypted_plain_text = "" - for binary_ascii in decrypted_in_binary_ascii: + for binary_ascii in result_decrypt: decrypted_plain_text += transform_ascii_to_char(binary_ascii) - - return decrypted_in_binary_ascii, decrypted_plain_text + return result_decrypt, decrypted_plain_text def _confussion_decrypt(binary_char1: str, binary_char2: str, key_length: int) -> str: @@ -50,8 +48,31 @@ def _confussion_decrypt(binary_char1: str, binary_char2: str, key_length: int) - return xor_result -def _difussion_decrypt(encrypted_message: str, key: str) -> str: - """ - TODO - """ - pass \ No newline at end of file +def _diffusion_decrypt_transposition(binary_msg: str, key: str): + + kn = len(key) + result = binary_msg[kn:] + binary_msg[:kn] + + return result + +def _diffusion_decrypt_shift(binary_msg: str, charKey: str): + decimal_ascii_code = str(int(charKey, 2)) + + number_of_shifts = sum([int(i) for i in decimal_ascii_code]) + + number_of_shifts = (number_of_shifts - 8 if number_of_shifts >= 9 else number_of_shifts) + + # print("number_of_shifts: ", number_of_shifts) + + return shift_left(binary_msg, number_of_shifts) + + # shifts = 0 + # nrs = str(int(charKey, 2)) # Number of shifts + # for i in range(0, len(nrs)): + # if (shifts <= 7): + # shifts += int(nrs[i]) + # shifts = (shifts - 8 if shifts >= 9 else shifts) + + # binary_msg = shift(encrypted_message, shifts) + # return binary_msg + diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index eb6ada6..b65b8d6 100644 --- a/src/algorithms/encrypt.py +++ b/src/algorithms/encrypt.py @@ -1,7 +1,6 @@ -from src.utils.binary import transform_char_to_ascii, multiply_binary, xor_binary_values +from src.utils.binary import transform_char_to_ascii, multiply_binary, xor_binary_values, shift_right import base64 - def encrypt_message(plain_text: str, key: str) -> str: key_ascii = [] plain_text_ascii = [] @@ -12,36 +11,55 @@ def encrypt_message(plain_text: str, key: str) -> str: for char in plain_text: plain_text_ascii.append(transform_char_to_ascii(char)) - - # Uncomment this to see in the console the ORDER - # in which the operations are being done - # for char_message in plain_text: - # for key_char in key: - # print(char_message, key_char) for char_message in plain_text_ascii: operator = char_message for char_key in key_ascii: confussion_result_encrypt = _confussion_encrypt(operator, char_key, len(key)) - # TODO: Call Difussion decrypt here with the result of the confussion - # Then assign the result of the difussion to the operator - operator = confussion_result_encrypt - - binary_array_result.append(confussion_result_encrypt) - - base64_result = base64.b64encode(str(binary_array_result).encode('utf-8')) + # diffusion_result_encrypt = _diffusion_encrypt_shift(operator, char_key) + operator = confussion_result_encrypt # cambiar a diffusion_result_encrypt + + binary_array_result.append(operator) + + # Apply transposition layer encryption + result_encrypt = _diffusion_encrypt_transposition(binary_array_result, key) + + base64_result = base64.b64encode(str(result_encrypt).encode('utf-8')) - return binary_array_result, base64_result + return result_encrypt, base64_result def _confussion_encrypt(binary_char1: str, binary_char2: str, key_length: int): xor_result = xor_binary_values(binary_char1, binary_char2) multiplied_result = multiply_binary(xor_result, key_length) - return multiplied_result - -def _difussion_encrypt(plain_text: str, key: str) -> str: - """ - TODO - """ - pass \ No newline at end of file +def _diffusion_encrypt_transposition(binary_msg: str, key: str): + + kn = len(key) + result = binary_msg[-kn:] + binary_msg[:-kn] + + return result + +def _diffusion_encrypt_shift(binary_msg, charKey: str): + + decimal_ascii_code = str(int(charKey, 2)) + + number_of_shifts = sum([int(i) for i in decimal_ascii_code]) + + number_of_shifts = (number_of_shifts - 8 if number_of_shifts >= 9 else number_of_shifts) + + # print("number_of_shifts: ", number_of_shifts) + + return shift_right(binary_msg, number_of_shifts) + + # shifts = 0 + # nrs = str(int(charKey, 2)) # Number of shifts + # for i in range(0, len(nrs)): + # if (shifts <= 7): + # shifts += int(nrs[i]) + # shifts = (shifts - 8 if shifts >= 9 else shifts) + + # shifted_binary = shift(binary_msg, shifts) + # return shifted_binary + + \ No newline at end of file diff --git a/src/utils/binary.py b/src/utils/binary.py index c831522..657a0bf 100644 --- a/src/utils/binary.py +++ b/src/utils/binary.py @@ -24,20 +24,7 @@ def divide_binary(binary_result: str, key_length: int) -> int: divided_result = int(binary_result, 2) // int(key_binary, 2) - return bin(divided_result) - - # print("divided_result: ", bin(divided_result)) - - # mod = int(binary_result, 2) % int(key_binary, 2) - - # multiplication = multiply_binary(bin(divided_result), key_length) - - - # # this is multiplication + mod, both in bin representation - # binary_result = int(multiplication, 2) + int(bin(mod), 2) - - # return bin(binary_result) def xor_binary_values(binary_char1: str, binary_char2: str) -> str: """ @@ -48,14 +35,34 @@ def xor_binary_values(binary_char1: str, binary_char2: str) -> str: result = "0b00000011" """ - # binary_char1 = f"{ord(char1):08b}" - # binary_char2 = f"{ord(char2):08b}" - xor_decimal = int(binary_char1, 2) ^ int(binary_char2, 2) xor_result_binary = bin(xor_decimal) return xor_result_binary + +def shift_right(binary_string, shifts: int) -> str: + binary_string = binary_string[2:] + + for _i in range(0, shifts): + binary_string = binary_string[-1] + binary_string[:-1] + + return f"0b{binary_string}" + + # shift right bitwise + # return bin(int(binary_string, 2) >> shifts) + +def shift_left(binary_string: str, shifts: int) -> str: + + binary_string = binary_string[2:] + + for _i in range(0, shifts): + binary_string = binary_string[1:] + binary_string[0] + + return f"0b{binary_string}" + + # shift left bitwise + # return bin(int(binary_string, 2) << shifts) # TESTS # Encrypt