Compare commits
19 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
976c22b348 | 1 year ago |
|
|
ba1e254a3c | 1 year ago |
|
|
73f8204065 | 1 year ago |
|
|
00ff7845d9 | 1 year ago |
|
|
3aaa37ea8e | 1 year ago |
|
|
dd24e337e1 | 1 year ago |
|
|
806f03a5d4 | 1 year ago |
|
|
aa6685adb1 | 1 year ago |
|
|
d0a27a7752 | 1 year ago |
|
|
49796bbc0a | 1 year ago |
|
|
dc7e06cb15 | 1 year ago |
|
|
9a7e7d3c5d | 1 year ago |
|
|
d9cdb7ce7f | 1 year ago |
|
|
d8c829c4cd | 1 year ago |
|
|
928d48f280 | 1 year ago |
|
|
ed67d86351 | 1 year ago |
|
|
fe0f4a51df | 1 year ago |
|
|
cd9b228ac3 | 1 year ago |
|
|
b7180991a8 | 1 year ago |
@ -0,0 +1,21 @@
|
||||
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')
|
||||
@ -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,61 @@
|
||||
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}")
|
||||
@ -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.")
|
||||
@ -0,0 +1,50 @@
|
||||
|
||||
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
|
||||
@ -0,0 +1,45 @@
|
||||
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…
Reference in new issue