From d8c829c4cd7b6c4a6f560a994c9ded612ee39f7a Mon Sep 17 00:00:00 2001 From: wdr <19978377276@163.com> Date: Wed, 25 Dec 2024 21:57:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Initialization.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Initialization.py diff --git a/Initialization.py b/Initialization.py new file mode 100644 index 0000000..ba4ef81 --- /dev/null +++ b/Initialization.py @@ -0,0 +1,54 @@ +## Python file to initialize the overall progress of the file transmit +## Finished May 31 2:20 p.m. + +import os +import shutil + + +def does_not_exist(directory_path): + if os.path.exists(directory_path): + return False + return True + + +def not_empty_dir(directory_path): + if len(os.listdir(directory_path)) == 0: + return False + return True + + +if does_not_exist('inbox'): + os.mkdir('inbox') + +if does_not_exist('outbox'): + os.mkdir('outbox') + +if does_not_exist('sent'): + os.mkdir('sent') + +if does_not_exist('sandbox_receiver'): + os.mkdir('sandbox_receiver') + +if not_empty_dir('inbox'): + shutil.rmtree(r'inbox') + os.mkdir(r'inbox') + +if not_empty_dir('sent'): + shutil.rmtree(r'sent') + os.mkdir(r'sent') + +if not_empty_dir('outbox'): + shutil.rmtree(r'outbox') + os.mkdir(r'outbox') + +try: + os.unlink('public_key_sender.txt') + os.unlink('public_key_sender.pem') + os.unlink('public_key_receiver.txt') + os.unlink('sandbox_sender/private_key_sender.txt') + os.unlink('sandbox_sender/private_key_sender.pem') + os.unlink('sandbox_receiver/private_key_receiver.txt') +except Exception as ex: + print("文件已被初始化") + +print('Initialization Complete') \ No newline at end of file -- 2.34.1 From d9cdb7ce7fa12d155415c1635de3d883be98ba25 Mon Sep 17 00:00:00 2001 From: wdr <19978377276@163.com> Date: Wed, 25 Dec 2024 22:06:25 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sender.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sender.py diff --git a/sender.py b/sender.py new file mode 100644 index 0000000..4b9e18b --- /dev/null +++ b/sender.py @@ -0,0 +1,35 @@ +def message_encrypt(): + encrypted_lines = "" + + # 读取 DES 密钥 + with open('sandbox_sender/desPassword.txt', 'r') as des_password_gotcha: + des_pwd = des_password_gotcha.readline().strip() # 移除可能存在的换行符 + + # 确保 DES 密钥长度为 8 字节 + if len(des_pwd) != 8: + raise ValueError("DES 密钥长度必须是 8 字节") + + # 读取原始消息 + with open('sandbox_sender/sample.txt', 'r') as original_file: + original = original_file.read() + + # 使用 PKCS#7 填充,使文本长度为 DES 块大小的倍数 (8 字节) + original_length = len(original) + if original_length % 8 != 0: + padding_length = 8 - (original_length % 8) + original = original + chr(padding_length) * padding_length + + # 创建 DES 加密器 + des_cipher = DES.new(des_pwd.encode(), DES.MODE_ECB) # 使用 ECB 模式 + + # 加密文本 + encrypted_lines = "" + for i in range(0, len(original), 8): + block = original[i:i + 8].encode() # 取每个 8 字节块 + encrypted_block = des_cipher.encrypt(block) # 加密块 + encrypted_lines += encrypted_block.hex() # 将加密结果转换为十六进制字符串 + + # 将加密后的消息保存到文件 + with open('outbox/encrypted_msg.txt', 'w') as container_of_encrypted_contents: + container_of_encrypted_contents.write(encrypted_lines) + print("Message has been encrypted.") \ No newline at end of file -- 2.34.1 From 9a7e7d3c5d751767a5c71cc211a6c1f89f30b519 Mon Sep 17 00:00:00 2001 From: wdr <19978377276@163.com> Date: Wed, 25 Dec 2024 22:43:07 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- new sender.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 new sender.py diff --git a/new sender.py b/new sender.py new file mode 100644 index 0000000..5215f1e --- /dev/null +++ b/new sender.py @@ -0,0 +1,51 @@ +def des_key_encrypt_with_rsa(): + # 读取 DES 密钥 + with open('sandbox_sender/desPassword.txt', 'r') as des_password_gotcha: + des_pwd = des_password_gotcha.readline().strip() # 移除换行符 + + # 读取公钥 + with open('public_key_receiver.txt', 'r') as pub_key_radio: + public_key_pair_string = pub_key_radio.readline().strip() # 移除换行符 + + # 假设公钥是以 `(n, e)` 格式存储 + try: + public_key_pair_string2pair_step_1 = public_key_pair_string.split(', ') + + public_key_pair_string2pair_step_2_a = public_key_pair_string2pair_step_1[0] + if public_key_pair_string2pair_step_2_a.startswith('('): + public_key_pair_string2pair_step_2_a = public_key_pair_string2pair_step_2_a[1:] + + public_key_pair_string2pair_step_2_b = public_key_pair_string2pair_step_1[1] + if public_key_pair_string2pair_step_2_b.endswith(')'): + public_key_pair_string2pair_step_2_b = public_key_pair_string2pair_step_2_b[:-1] + + # 转换成整数 + n = int(public_key_pair_string2pair_step_2_a) + e = int(public_key_pair_string2pair_step_2_b) + + # 打印输出以验证解析是否正确 + #print(f"Parsed RSA public key: n = {n}, e = {e}") + except Exception as ex: + print(f"Error parsing the RSA public key: {ex}") + return + + # 创建 RSA 公钥对象 + try: + rsa_public_key = RSA.construct((n, e)) # 使用解析出的 (n, e) 创建 RSA 公钥 + rsa_cipher = PKCS1_OAEP.new(rsa_public_key) + + # 将 DES 密钥字符串编码为字节 + des_pwd_bytes = des_pwd.encode() + + # 使用 RSA 加密 DES 密钥 + encrypted_des_pwd = rsa_cipher.encrypt(des_pwd_bytes) + + # 将加密后的 DES 密钥转换为十六进制字符串 + encrypted_des_pwd_hex = encrypted_des_pwd.hex() + + # 将加密后的 DES 密钥保存到文件 + with open('outbox/encrypted_pwd_des.txt', 'w') as encrypted_des_pwd_container: + encrypted_des_pwd_container.write(encrypted_des_pwd_hex) + print("DES password has been encrypted.") + except ValueError as ve: + print(f"Error while constructing RSA public key: {ve}") \ No newline at end of file -- 2.34.1