初始化 发送方加密明文和加密密钥 #1

Merged
psyfbu5z8 merged 3 commits from weidr into main 1 year ago

@ -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')

@ -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}")

@ -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.")
Loading…
Cancel
Save