Compare commits

..

No commits in common. 'main' and '12' have entirely different histories.
main ... 12

@ -1,46 +0,0 @@
def des_pwd_decrypt():
with open('sent/encrypted_pwd_des.txt', 'r') as des_pwd_container:
des_pwd_encrypted = des_pwd_container.readline()
with open('sandbox_receiver/private_key_receiver.txt', 'r') as prv_key_radio:
private_key_pair_string = prv_key_radio.readline()
private_key_pair_string2pair_step_1 = private_key_pair_string.split(', ')
# 手动实现removeprefix
def remove_prefix(s, prefix):
if s.startswith(prefix):
return s[len(prefix):]
return s
# 手动实现removesuffix
def remove_suffix(s, suffix):
if s.endswith(suffix):
return s[:-len(suffix)]
return s
# 修改原代码
private_key_pair_string2pair_step_2_a = remove_prefix(private_key_pair_string2pair_step_1[0], '(')
private_key_pair_string2pair_step_2_b = remove_suffix(private_key_pair_string2pair_step_1[1], ')')
private_key_pair_string2pair_step_2_c = remove_suffix(private_key_pair_string2pair_step_1[2], ')')
private_key_pair = int(private_key_pair_string2pair_step_2_a), int(private_key_pair_string2pair_step_2_b),int(private_key_pair_string2pair_step_2_c)
#print(private_key_pair)
rsa_private_key = RSA.construct(private_key_pair)# 使用解析出的 (n, e) 创建 RSA 公钥
rsa_cipher = PKCS1_OAEP.new(rsa_private_key)
# rsa_private_key = public_key.save_pkcs1()
# 将 DES 密钥字符串编码为字节
des_pwd_encrypted_bytes = bytes.fromhex(des_pwd_encrypted)
# 使用 RSA 加密 DES 密钥
decrypted_des_pwd = rsa_cipher.decrypt(des_pwd_encrypted_bytes)
with open('inbox/desPassword.txt', 'wb') as des_password_file:
des_password_file.write(decrypted_des_pwd)
#print(decrypted_des_pwd)
# 将加密后的 DES 密钥转换为十六进制字符串
encrypted_des_pwd_hex = decrypted_des_pwd.hex()
return decrypted_des_pwd

@ -1,29 +0,0 @@
def message_decrypt():
decrypted_lines = ""
with open('sandbox_sender/desPassword.txt', 'r') as des_password_gotcha:
des_pwd = des_pwd_decrypt()
# 读取加密的消息(十六进制格式)
with open('sent/encrypted_msg.txt', 'r') as encrypted_file:
encrypted_lines = encrypted_file.read()
# 将十六进制字符串转换为二进制数据
encrypted_data = binascii.unhexlify(encrypted_lines)
# 创建 DES 解密器
des_cipher = DES.new(des_pwd, DES.MODE_ECB) # 使用 ECB 模式
# 解密文本
decrypted_message = des_cipher.decrypt(encrypted_data).decode()
# 移除 PKCS#7 填充
padding_length = ord(decrypted_message[-1]) # 填充长度是最后一个字符的 ASCII 值
decrypted_message = decrypted_message[:-padding_length] # 去除填充
# 打印解密后的消息
print("Decrypted Message:")
print(decrypted_message)
# 可选择将解密后的消息保存到文件
with open('inbox/decrypted_msg.txt', 'w') as decrypted_file:
decrypted_file.write(decrypted_message)
print("Decrypted message saved to 'decrypted_msg.txt'.")

@ -1,54 +0,0 @@
## 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')

@ -1,61 +0,0 @@
import binascii
import hashlib
import base64
from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
import rsa
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}")

@ -1,51 +0,0 @@
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}")

@ -1,35 +0,0 @@
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.")

@ -1,50 +0,0 @@
import hashlib
from Crypto.PublicKey import RSA
from Crypto.Cipher import DES
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from Crypto.Cipher import PKCS1_OAEP
def sign():
# 读取文件中的消息内容
with open('sandbox_sender/sample.txt', 'rb') as original_file:
message = original_file.read() # 以二进制方式读取消息
# 读取私钥文件并加载私钥
with open('sandbox_sender/private_key_sender.pem', 'rb') as private_key_file:
private_key = serialization.load_pem_private_key(
private_key_file.read(),
password=None,
backend=default_backend()
)
# 创建消息的哈希
message_hash = hashes.Hash(hashes.SHA256(), backend=default_backend())
message_hash.update(message) # 使用文件内容更新哈希
digest = message_hash.finalize()
# 使用 PSS 填充对消息进行签名
signature = private_key.sign(
digest,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
# 将签名转换为十六进制字符串
signature_hex = signature.hex()
# 打印签名
#print(f"Signature (Hex): {signature_hex}")
# 保存签名为十六进制字符串到文件
with open('outbox/signature.txt', 'w') as f:
f.write(signature_hex)
print("Signature saved as hex in 'outbox/signature.txt'.")
return signature

@ -1,45 +0,0 @@
from Crypto.Cipher import DES
import binascii
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
def verify():
# 读取解密后的消息内容
with open('inbox/decrypted_msg.txt', 'rb') as decrypted_file:
message = decrypted_file.read() # 以二进制方式读取消息
# 从 PEM 文件中加载公钥
with open('public_key_sender.pem', 'rb') as f: # 以二进制模式打开
public_key = serialization.load_pem_public_key(f.read(), backend=default_backend())
# 读取十六进制格式的签名
with open('sent/signature.txt', 'r') as signature_file:
hex_signature = signature_file.read().strip() # 读取并去掉两端的空白字符
# 将十六进制签名转换为字节数据
signature = bytes.fromhex(hex_signature)
# 计算消息的哈希(应与签名时一致)
message_hash = hashes.Hash(hashes.SHA256(), backend=default_backend())
message_hash.update(message)
digest = message_hash.finalize()
try:
# 使用公钥对签名进行验证
public_key.verify(
signature,
digest, # 使用哈希值进行验证
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print("Signature verified successfully.")
return True # 验证成功
except Exception as e:
print(f"Verification failed: {e}")
return False # 验证失败

Binary file not shown.
Loading…
Cancel
Save