Compare commits

..

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

21
11.py

@ -1,21 +0,0 @@
import os
import shutil
import time
shutil.rmtree(r'inbox')
os.makedirs('inbox')
willingness = input("Are you sure to send all files in the outbox?\nNO(0)/YES(1)>>> ")
if willingness == "1":
print("Sending", end=">")
time.sleep(1)
for i in range(10):
print(">", end="")
time.sleep(0.2)
shutil.move('outbox/encrypted_hash.txt', r'sent/encrypted_hash.txt')
shutil.move('outbox/encrypted_msg.txt', r'sent/encrypted_msg.txt')
shutil.move('outbox/encrypted_pwd_des.txt', r'sent/encrypted_pwd_des.txt')
print("\rPack sent.")
else:
os.unlink(r'outbox/encrypted_hash.txt')
os.unlink(r'outbox/encrypted_msg.txt')
os.unlink(r'outbox/encrypted_pwd_des.txt')

@ -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,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.")

Binary file not shown.
Loading…
Cancel
Save