2020-05-01 21:36:35 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2020-09-28 21:41:04 +00:00
|
|
|
from . import rsa_key_generator as rkg
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
DEFAULT_BLOCK_SIZE = 128
|
|
|
|
BYTE_SIZE = 256
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def get_blocks_from_text(
|
|
|
|
message: str, block_size: int = DEFAULT_BLOCK_SIZE
|
|
|
|
) -> list[int]:
|
|
|
|
message_bytes = message.encode("ascii")
|
|
|
|
|
|
|
|
block_ints = []
|
|
|
|
for block_start in range(0, len(message_bytes), block_size):
|
|
|
|
block_int = 0
|
|
|
|
for i in range(block_start, min(block_start + block_size, len(message_bytes))):
|
|
|
|
block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
|
|
|
|
block_ints.append(block_int)
|
|
|
|
return block_ints
|
|
|
|
|
|
|
|
|
|
|
|
def get_text_from_blocks(
|
|
|
|
block_ints: list[int], message_length: int, block_size: int = DEFAULT_BLOCK_SIZE
|
2020-10-16 06:11:52 +00:00
|
|
|
) -> str:
|
2021-04-04 05:22:12 +00:00
|
|
|
message: list[str] = []
|
|
|
|
for block_int in block_ints:
|
|
|
|
block_message: list[str] = []
|
|
|
|
for i in range(block_size - 1, -1, -1):
|
|
|
|
if len(message) + i < message_length:
|
2022-01-30 19:29:54 +00:00
|
|
|
ascii_number = block_int // (BYTE_SIZE**i)
|
|
|
|
block_int = block_int % (BYTE_SIZE**i)
|
2021-04-04 05:22:12 +00:00
|
|
|
block_message.insert(0, chr(ascii_number))
|
|
|
|
message.extend(block_message)
|
2019-10-05 05:14:13 +00:00
|
|
|
return "".join(message)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def encrypt_message(
|
2022-10-12 22:54:20 +00:00
|
|
|
message: str, key: tuple[int, int], block_size: int = DEFAULT_BLOCK_SIZE
|
2021-04-04 05:22:12 +00:00
|
|
|
) -> list[int]:
|
|
|
|
encrypted_blocks = []
|
2016-09-06 12:23:48 +00:00
|
|
|
n, e = key
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
for block in get_blocks_from_text(message, block_size):
|
2021-04-04 05:22:12 +00:00
|
|
|
encrypted_blocks.append(pow(block, e, n))
|
|
|
|
return encrypted_blocks
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def decrypt_message(
|
|
|
|
encrypted_blocks: list[int],
|
|
|
|
message_length: int,
|
|
|
|
key: tuple[int, int],
|
|
|
|
block_size: int = DEFAULT_BLOCK_SIZE,
|
2020-10-16 06:11:52 +00:00
|
|
|
) -> str:
|
2021-04-04 05:22:12 +00:00
|
|
|
decrypted_blocks = []
|
2016-09-06 12:23:48 +00:00
|
|
|
n, d = key
|
2021-04-04 05:22:12 +00:00
|
|
|
for block in encrypted_blocks:
|
|
|
|
decrypted_blocks.append(pow(block, d, n))
|
|
|
|
return get_text_from_blocks(decrypted_blocks, message_length, block_size)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def read_key_file(key_filename: str) -> tuple[int, int, int]:
|
|
|
|
with open(key_filename) as fo:
|
2019-01-08 08:59:23 +00:00
|
|
|
content = fo.read()
|
2022-10-12 22:54:20 +00:00
|
|
|
key_size, n, eor_d = content.split(",")
|
|
|
|
return (int(key_size), int(n), int(eor_d))
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def encrypt_and_write_to_file(
|
|
|
|
message_filename: str,
|
|
|
|
key_filename: str,
|
2020-10-16 06:11:52 +00:00
|
|
|
message: str,
|
2021-04-04 05:22:12 +00:00
|
|
|
block_size: int = DEFAULT_BLOCK_SIZE,
|
2020-10-16 06:11:52 +00:00
|
|
|
) -> str:
|
2021-04-04 05:22:12 +00:00
|
|
|
key_size, n, e = read_key_file(key_filename)
|
|
|
|
if key_size < block_size * 8:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit(
|
2023-04-24 05:28:30 +00:00
|
|
|
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
|
2020-05-01 21:36:35 +00:00
|
|
|
"requires the block size to be equal to or greater than the key size. "
|
2023-04-24 05:28:30 +00:00
|
|
|
"Either decrease the block size or use different keys.".format(
|
|
|
|
block_size * 8, key_size
|
|
|
|
)
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)]
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
encrypted_content = ",".join(encrypted_blocks)
|
|
|
|
encrypted_content = f"{len(message)}_{block_size}_{encrypted_content}"
|
|
|
|
with open(message_filename, "w") as fo:
|
|
|
|
fo.write(encrypted_content)
|
|
|
|
return encrypted_content
|
2016-09-06 12:23:48 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str:
|
|
|
|
key_size, n, d = read_key_file(key_filename)
|
|
|
|
with open(message_filename) as fo:
|
2019-01-08 08:59:23 +00:00
|
|
|
content = fo.read()
|
2021-04-04 05:22:12 +00:00
|
|
|
message_length_str, block_size_str, encrypted_message = content.split("_")
|
|
|
|
message_length = int(message_length_str)
|
|
|
|
block_size = int(block_size_str)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
if key_size < block_size * 8:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit(
|
2023-04-24 05:28:30 +00:00
|
|
|
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
|
2020-05-01 21:36:35 +00:00
|
|
|
"requires the block size to be equal to or greater than the key size. "
|
2023-04-24 05:28:30 +00:00
|
|
|
"Did you specify the correct key file and encrypted file?".format(
|
|
|
|
block_size * 8, key_size
|
|
|
|
)
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
encrypted_blocks = []
|
|
|
|
for block in encrypted_message.split(","):
|
|
|
|
encrypted_blocks.append(int(block))
|
|
|
|
|
|
|
|
return decrypt_message(encrypted_blocks, message_length, (n, d), block_size)
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
filename = "encrypted_file.txt"
|
|
|
|
response = input(r"Encrypt\Decrypt [e\d]: ")
|
|
|
|
|
|
|
|
if response.lower().startswith("e"):
|
|
|
|
mode = "encrypt"
|
|
|
|
elif response.lower().startswith("d"):
|
|
|
|
mode = "decrypt"
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
if mode == "encrypt":
|
|
|
|
if not os.path.exists("rsa_pubkey.txt"):
|
2022-10-12 22:54:20 +00:00
|
|
|
rkg.make_key_files("rsa", 1024)
|
2021-04-04 05:22:12 +00:00
|
|
|
|
|
|
|
message = input("\nEnter message: ")
|
|
|
|
pubkey_filename = "rsa_pubkey.txt"
|
2022-07-07 14:34:07 +00:00
|
|
|
print(f"Encrypting and writing to {filename}...")
|
2022-10-12 22:54:20 +00:00
|
|
|
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)
|
2021-04-04 05:22:12 +00:00
|
|
|
|
|
|
|
print("\nEncrypted text:")
|
2022-10-12 22:54:20 +00:00
|
|
|
print(encrypted_text)
|
2021-04-04 05:22:12 +00:00
|
|
|
|
|
|
|
elif mode == "decrypt":
|
|
|
|
privkey_filename = "rsa_privkey.txt"
|
2022-07-07 14:34:07 +00:00
|
|
|
print(f"Reading from {filename} and decrypting...")
|
2021-04-04 05:22:12 +00:00
|
|
|
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
|
|
|
|
print("writing decryption to rsa_decryption.txt...")
|
|
|
|
with open("rsa_decryption.txt", "w") as dec:
|
|
|
|
dec.write(decrypted_text)
|
|
|
|
|
|
|
|
print("\nDecryption:")
|
|
|
|
print(decrypted_text)
|
2016-09-06 12:23:48 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-09-06 12:23:48 +00:00
|
|
|
main()
|