From afcdaedfebedd5d6b1fafd5422d71fcf0785a930 Mon Sep 17 00:00:00 2001 From: esquivelgor Date: Sun, 17 Sep 2023 02:10:58 -0600 Subject: [PATCH 1/5] test --- src/algorithms/decrypt.py | 34 ++++++++++++++++++++++++---------- src/algorithms/encrypt.py | 37 +++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index bc17214..b3de173 100644 --- a/src/algorithms/decrypt.py +++ b/src/algorithms/decrypt.py @@ -21,14 +21,13 @@ 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)) + difussion_decrypt_result = _diffusion_decrypt(operator, char_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) - + decrypted_plain_text = "" for binary_ascii in decrypted_in_binary_ascii: decrypted_plain_text += transform_ascii_to_char(binary_ascii) @@ -43,8 +42,23 @@ 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(encrypted_message: str, key: str): + # Number of left shifts + nrs = str(int(key, 2)) + shifts = 0 + for i in range(0, len(nrs)): + if (shifts <= 7): + shifts += int(nrs[i]) + + shifts = (shifts if shifts <= 8 else shifts - 8) + + decimal_number_shifted = int(encrypted_message, 2) + + decimal_number = decimal_number_shifted << shifts + + binary_msg = bin(decimal_number) + print(shifts) + print(encrypted_message) + print(binary_msg) + print("") + return encrypted_message diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index eb6ada6..f45cc62 100644 --- a/src/algorithms/encrypt.py +++ b/src/algorithms/encrypt.py @@ -23,11 +23,11 @@ def encrypt_message(plain_text: str, key: str) -> str: 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 + difussion_result_encrypt = _difussion_encrypt(confussion_result_encrypt, char_key) - binary_array_result.append(confussion_result_encrypt) + operator = difussion_result_encrypt + + binary_array_result.append(operator) base64_result = base64.b64encode(str(binary_array_result).encode('utf-8')) @@ -36,12 +36,29 @@ def encrypt_message(plain_text: str, key: str) -> str: 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 _difussion_encrypt(binary_msg: str, key_char: str): + # Number of right shifts + nrs = str(int(key_char, 2)) + shifts = 0 + + for i in range(0, len(nrs)): + if (shifts <= 7): + shifts += int(nrs[i]) + # (if n < 0 then shifting by 2 positions) + shifts = (shifts if shifts <= 8 else shifts - 8) + + # Convert binary string to an integer + decimal_number = int(binary_msg, 2) + # Perform a bitwise shift right operation + shifted_decimal = decimal_number >> shifts + # Convert the shifted decimal number back to a binary string + shifted_binary = bin(shifted_decimal) + + print(shifts) + print(binary_msg) + print(shifted_binary) + print("") + return shifted_binary \ No newline at end of file From 527af1b6738268cf1446c206db5d73379e53f68a Mon Sep 17 00:00:00 2001 From: esquivelgor Date: Sun, 17 Sep 2023 23:12:10 -0600 Subject: [PATCH 2/5] changes --- src/algorithms/decrypt.py | 56 ++++++++++++++++++++++++---------- src/algorithms/encrypt.py | 64 +++++++++++++++++++++++++++------------ 2 files changed, 84 insertions(+), 36 deletions(-) diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index b3de173..64bbd09 100644 --- a/src/algorithms/decrypt.py +++ b/src/algorithms/decrypt.py @@ -21,13 +21,15 @@ 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)) - difussion_decrypt_result = _diffusion_decrypt(operator, char_key) - - operator = confussion_decrypt_result + difussion_decrypt_result = _diffusion_decrypt_shift(confussion_decrypt_result, char_key) + #print(confussion_decrypt_result) + #print(difussion_decrypt_result) + operator = confussion_decrypt_result # debe de ir diffusion_decrypt_result - decrypted_in_binary_ascii.append(operator) + decrypted_in_binary_ascii.append(operator) + #difussion_decrypt_result.append(_diffusion_decrypt_rotation(decrypted_in_binary_ascii, key)) decrypted_plain_text = "" for binary_ascii in decrypted_in_binary_ascii: decrypted_plain_text += transform_ascii_to_char(binary_ascii) @@ -42,23 +44,45 @@ def _confussion_decrypt(binary_char1: str, binary_char2: str, key_length: int) - return xor_result -def _diffusion_decrypt(encrypted_message: str, key: str): - # Number of left shifts - nrs = str(int(key, 2)) +def _diffusion_decrypt_shift(encrypted_message: str, charKey: str): + shifts = 0 + nrs = str(int(charKey, 2)) # Number of left 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 + +def _diffusion_decrypt_rotation(binary_msg: str, key: str): + + # We get all concatenation of values + fullBinaryMsg = list(''.join(binary_msg)) + + n = str(int(transform_char_to_ascii(key[-1]),2)) + kn = 0 + kn = sum(int(n[i]) for i in range(len(n))) + kn = int(str(kn)[-1]) - shifts = (shifts if shifts <= 8 else shifts - 8) + rotatedBinary = (fullBinaryMsg[:kn] + fullBinaryMsg[kn:]) + result = ''.join(rotatedBinary) + print(result, fullBinaryMsg) + return 1 - decimal_number_shifted = int(encrypted_message, 2) +# Shift left +def shift(binary_string: str, shifts: int): + #print(binary_string) + binary_string = str(format(int(binary_string,2), '08b')) - decimal_number = decimal_number_shifted << shifts + #print(binary_string) + firstPart = binary_string[:-shifts:] + rest = binary_string[-shifts:] + shifted = rest + firstPart + #print(shifted) + + shifted_binary = bin(int(shifted, 2)) + #print(shifted_binary) + return shifted_binary - binary_msg = bin(decimal_number) - print(shifts) - print(encrypted_message) - print(binary_msg) - print("") - return encrypted_message diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index f45cc62..4904ed0 100644 --- a/src/algorithms/encrypt.py +++ b/src/algorithms/encrypt.py @@ -23,13 +23,13 @@ def encrypt_message(plain_text: str, key: str) -> str: operator = char_message for char_key in key_ascii: confussion_result_encrypt = _confussion_encrypt(operator, char_key, len(key)) - difussion_result_encrypt = _difussion_encrypt(confussion_result_encrypt, char_key) + diffusion_result_encrypt = _diffusion_encrypt_shift(confussion_result_encrypt, char_key) - operator = difussion_result_encrypt + operator = confussion_result_encrypt # cambiar a diffusion_result_encrypt binary_array_result.append(operator) - - base64_result = base64.b64encode(str(binary_array_result).encode('utf-8')) + result_encrypt = _diffusion_encrypt_rotation(binary_array_result, key) + base64_result = base64.b64encode(str(result_encrypt).encode('utf-8')) return binary_array_result, base64_result @@ -39,26 +39,50 @@ def _confussion_encrypt(binary_char1: str, binary_char2: str, key_length: int): return multiplied_result -def _difussion_encrypt(binary_msg: str, key_char: str): - # Number of right shifts - nrs = str(int(key_char, 2)) +def _diffusion_encrypt_shift(binary_msg: str, charKey: str): + shifts = 0 - + nrs = str(int(charKey, 2)) # Number of right shifts for i in range(0, len(nrs)): if (shifts <= 7): shifts += int(nrs[i]) - # (if n < 0 then shifting by 2 positions) - shifts = (shifts if shifts <= 8 else shifts - 8) + shifts = (shifts - 8 if shifts >= 9 else shifts) + + shifted_binary = shift(binary_msg, shifts) + #print("----------") + #print(shifts) + #print(binary_msg) + #print(shifted_binary) + #print("----------") + return shifted_binary + +def _diffusion_encrypt_rotation(binary_msg: str, key: str): + + # We get all concatenation of values + fullBinaryMsg = list(''.join(binary_msg)) + + n = str(int(transform_char_to_ascii(key[-1]),2)) + kn = 0 + kn = sum(int(n[i]) for i in range(len(n))) + kn = int(str(kn)[-1]) - # Convert binary string to an integer - decimal_number = int(binary_msg, 2) - # Perform a bitwise shift right operation - shifted_decimal = decimal_number >> shifts - # Convert the shifted decimal number back to a binary string - shifted_binary = bin(shifted_decimal) + + rotatedBinary = (fullBinaryMsg[kn:] + fullBinaryMsg[:kn]) + result = ''.join(rotatedBinary) + print(result, fullBinaryMsg) + return result - print(shifts) - print(binary_msg) - print(shifted_binary) - print("") +# Shift right +def shift(binary_string: str, shifts: int): + #print(binary_string) + binary_string = str(format(int(binary_string,2), '08b')) + + #print(binary_string) + firstPart = binary_string[:shifts] + rest = binary_string[shifts:] + shifted = rest + firstPart + #print(shifted) + + shifted_binary = bin(int(shifted, 2)) + #print(shifted_binary) return shifted_binary \ No newline at end of file From 8d6a6ed23141c4032f08e78040bfbe91a7261e81 Mon Sep 17 00:00:00 2001 From: esquivelgor Date: Mon, 18 Sep 2023 20:47:28 -0600 Subject: [PATCH 3/5] 2nd diffusion method done --- src/algorithms/decrypt.py | 55 ++++++++++++-------------------------- src/algorithms/encrypt.py | 56 ++++++++++----------------------------- 2 files changed, 31 insertions(+), 80 deletions(-) diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index 64bbd09..ef9e086 100644 --- a/src/algorithms/decrypt.py +++ b/src/algorithms/decrypt.py @@ -12,7 +12,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)) @@ -21,21 +21,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)) - difussion_decrypt_result = _diffusion_decrypt_shift(confussion_decrypt_result, char_key) - - #print(confussion_decrypt_result) - #print(difussion_decrypt_result) + operator = confussion_decrypt_result # debe de ir diffusion_decrypt_result - decrypted_in_binary_ascii.append(operator) - - #difussion_decrypt_result.append(_diffusion_decrypt_rotation(decrypted_in_binary_ascii, key)) + binary_ascii.append(operator) + result_decrypt = _diffusion_encrypt_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: @@ -44,6 +41,13 @@ def _confussion_decrypt(binary_char1: str, binary_char2: str, key_length: int) - return xor_result +def _diffusion_encrypt_transposition(binary_msg: str, key: str): + + kn = len(key) + result = binary_msg[kn:] + binary_msg[:kn] + + return result + def _diffusion_decrypt_shift(encrypted_message: str, charKey: str): shifts = 0 @@ -54,35 +58,10 @@ def _diffusion_decrypt_shift(encrypted_message: str, charKey: str): shifts = (shifts - 8 if shifts >= 9 else shifts) binary_msg = shift(encrypted_message, shifts) - return binary_msg + return encrypted_message -def _diffusion_decrypt_rotation(binary_msg: str, key: str): - - # We get all concatenation of values - fullBinaryMsg = list(''.join(binary_msg)) - - n = str(int(transform_char_to_ascii(key[-1]),2)) - kn = 0 - kn = sum(int(n[i]) for i in range(len(n))) - kn = int(str(kn)[-1]) +# Shift right (decrypt) +def shift(binary_string: str, shifts: int): - rotatedBinary = (fullBinaryMsg[:kn] + fullBinaryMsg[kn:]) - result = ''.join(rotatedBinary) - print(result, fullBinaryMsg) return 1 -# Shift left -def shift(binary_string: str, shifts: int): - #print(binary_string) - binary_string = str(format(int(binary_string,2), '08b')) - - #print(binary_string) - firstPart = binary_string[:-shifts:] - rest = binary_string[-shifts:] - shifted = rest + firstPart - #print(shifted) - - shifted_binary = bin(int(shifted, 2)) - #print(shifted_binary) - return shifted_binary - diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index 4904ed0..4ef4b08 100644 --- a/src/algorithms/encrypt.py +++ b/src/algorithms/encrypt.py @@ -12,23 +12,18 @@ 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)) - diffusion_result_encrypt = _diffusion_encrypt_shift(confussion_result_encrypt, char_key) operator = confussion_result_encrypt # cambiar a diffusion_result_encrypt binary_array_result.append(operator) - result_encrypt = _diffusion_encrypt_rotation(binary_array_result, key) + + # 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 @@ -37,7 +32,13 @@ 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 _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: str, charKey: str): @@ -49,40 +50,11 @@ def _diffusion_encrypt_shift(binary_msg: str, charKey: str): shifts = (shifts - 8 if shifts >= 9 else shifts) shifted_binary = shift(binary_msg, shifts) - #print("----------") - #print(shifts) - #print(binary_msg) - #print(shifted_binary) - #print("----------") - return shifted_binary -def _diffusion_encrypt_rotation(binary_msg: str, key: str): - - # We get all concatenation of values - fullBinaryMsg = list(''.join(binary_msg)) - - n = str(int(transform_char_to_ascii(key[-1]),2)) - kn = 0 - kn = sum(int(n[i]) for i in range(len(n))) - kn = int(str(kn)[-1]) - - - rotatedBinary = (fullBinaryMsg[kn:] + fullBinaryMsg[:kn]) - result = ''.join(rotatedBinary) - print(result, fullBinaryMsg) - return result + return binary_msg -# Shift right +# Shift left def shift(binary_string: str, shifts: int): - #print(binary_string) - binary_string = str(format(int(binary_string,2), '08b')) - - #print(binary_string) - firstPart = binary_string[:shifts] - rest = binary_string[shifts:] - shifted = rest + firstPart - #print(shifted) - shifted_binary = bin(int(shifted, 2)) - #print(shifted_binary) - return shifted_binary \ No newline at end of file + print(binary_string, char_key) + return 1 \ No newline at end of file From 4e5909111f57fdac6766e5a4f6d6e290c461ff4e Mon Sep 17 00:00:00 2001 From: esquivelgor Date: Mon, 18 Sep 2023 21:26:15 -0600 Subject: [PATCH 4/5] shift function --- src/algorithms/decrypt.py | 11 +++++------ src/algorithms/encrypt.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index ef9e086..cb49e46 100644 --- a/src/algorithms/decrypt.py +++ b/src/algorithms/decrypt.py @@ -21,8 +21,8 @@ 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)) - - operator = confussion_decrypt_result # debe de ir diffusion_decrypt_result + 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_encrypt_transposition(binary_ascii, key) @@ -51,17 +51,16 @@ def _diffusion_encrypt_transposition(binary_msg: str, key: str): def _diffusion_decrypt_shift(encrypted_message: str, charKey: str): shifts = 0 - nrs = str(int(charKey, 2)) # Number of left shifts + 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 encrypted_message + return binary_msg # Shift right (decrypt) def shift(binary_string: str, shifts: int): - - return 1 + return bin(int(binary_string, 2) >> shifts) diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index 4ef4b08..1e2c804 100644 --- a/src/algorithms/encrypt.py +++ b/src/algorithms/encrypt.py @@ -17,7 +17,7 @@ def encrypt_message(plain_text: str, key: str) -> str: operator = char_message for char_key in key_ascii: confussion_result_encrypt = _confussion_encrypt(operator, char_key, len(key)) - + diffusion_result_encrypt = _diffusion_encrypt_shift(operator, char_key) operator = confussion_result_encrypt # cambiar a diffusion_result_encrypt binary_array_result.append(operator) @@ -40,21 +40,19 @@ def _diffusion_encrypt_transposition(binary_msg: str, key: str): return result -def _diffusion_encrypt_shift(binary_msg: str, charKey: str): +def _diffusion_encrypt_shift(binary_msg, charKey: str): shifts = 0 - nrs = str(int(charKey, 2)) # Number of right shifts + 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 binary_msg + return shifted_binary # Shift left -def shift(binary_string: str, shifts: int): - - print(binary_string, char_key) - return 1 \ No newline at end of file +def shift(binary_string, shifts: int): + return bin(int(binary_string,2) << shifts) + \ No newline at end of file From b374273aae8dcd7976a16e3895964ce8938be0aa Mon Sep 17 00:00:00 2001 From: Odzen Date: Wed, 20 Sep 2023 04:18:41 -0600 Subject: [PATCH 5/5] functions shift working, but not integrated. Missing testing --- src/algorithms/decrypt.py | 39 ++++++++++++++++++++++----------------- src/algorithms/encrypt.py | 37 ++++++++++++++++++++++--------------- src/utils/binary.py | 39 +++++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/src/algorithms/decrypt.py b/src/algorithms/decrypt.py index f59c7da..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 @@ -28,17 +28,17 @@ 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)) - diffusion_decrypt_result = _diffusion_decrypt_shift(confussion_decrypt_result, char_key) + # 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_encrypt_transposition(binary_ascii, key) + + result_decrypt = _diffusion_decrypt_transposition(binary_ascii, key) decrypted_plain_text = "" for binary_ascii in result_decrypt: decrypted_plain_text += transform_ascii_to_char(binary_ascii) - return result_decrypt, decrypted_plain_text @@ -48,26 +48,31 @@ def _confussion_decrypt(binary_char1: str, binary_char2: str, key_length: int) - return xor_result -def _diffusion_encrypt_transposition(binary_msg: str, key: str): +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(encrypted_message: str, charKey: str): +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) - 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) + return shift_left(binary_msg, number_of_shifts) - binary_msg = shift(encrypted_message, shifts) - return binary_msg + # 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) -# Shift right (decrypt) -def shift(binary_string: str, shifts: int): - return bin(int(binary_string, 2) >> shifts) + # binary_msg = shift(encrypted_message, shifts) + # return binary_msg diff --git a/src/algorithms/encrypt.py b/src/algorithms/encrypt.py index 1e2c804..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 = [] @@ -17,16 +16,17 @@ def encrypt_message(plain_text: str, key: str) -> str: operator = char_message for char_key in key_ascii: confussion_result_encrypt = _confussion_encrypt(operator, char_key, len(key)) - diffusion_result_encrypt = _diffusion_encrypt_shift(operator, char_key) + # 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) @@ -42,17 +42,24 @@ def _diffusion_encrypt_transposition(binary_msg: str, key: str): def _diffusion_encrypt_shift(binary_msg, charKey: str): - 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) + 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) - shifted_binary = shift(binary_msg, shifts) - return shifted_binary + # print("number_of_shifts: ", number_of_shifts) + + return shift_right(binary_msg, number_of_shifts) -# Shift left -def shift(binary_string, shifts: int): - return bin(int(binary_string,2) << 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