|
|
|
@ -1,5 +1,9 @@
|
|
|
|
|
# 模式,文件,自己的公钥从哪里来,别人的公钥从哪里来
|
|
|
|
|
import pyfiglet
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from entity.Letter import Letter
|
|
|
|
|
from itsdangerous import base64_encode
|
|
|
|
@ -11,30 +15,42 @@ from tool.symmetric.SM4 import encrypt_ecb, decrypt_cbc_with_iv, encrypt_cbc_wit
|
|
|
|
|
from Crypto.Random import get_random_bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 两个变量,记录信封
|
|
|
|
|
# 三个全局变量,记录信封
|
|
|
|
|
letterWay = ""
|
|
|
|
|
letterMode = ""
|
|
|
|
|
letterSymKey = b""
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
# greet
|
|
|
|
|
print("")
|
|
|
|
|
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
|
|
|
|
greet = pyfiglet.figlet_format("File Secure Transfer", font="slant", width=250)
|
|
|
|
|
print(greet)
|
|
|
|
|
print(" <For Secure And Fast File Transfer>")
|
|
|
|
|
author = " <-Made By Li-Nuo-Cheng Tan-Jun-Wen Ren-Qing-Yu->"
|
|
|
|
|
print(author)
|
|
|
|
|
print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
|
|
|
|
|
|
|
|
|
letter = Letter()
|
|
|
|
|
# 用户输入各种数据填充letter字段
|
|
|
|
|
path = selectFile()
|
|
|
|
|
|
|
|
|
|
with open(path,"rb") as f:
|
|
|
|
|
data = f.read()
|
|
|
|
|
letter.fileName = "交给你了"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
letter.fileName = getFileName(path)
|
|
|
|
|
letter.recvPubKey = getRecvPubKey()
|
|
|
|
|
letter.senderPubKey = getSenderPubKey()
|
|
|
|
|
letter.fileBase64, akey = SymEncryption(base64_encode(data).decode("utf-8"),letterSymKey)
|
|
|
|
|
# data = "Hello, AES!"
|
|
|
|
|
letter.encryptKey = getEncryptKey()
|
|
|
|
|
letter.encryptType = getEncryptType()
|
|
|
|
|
|
|
|
|
|
letter.sign = getSign(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(letter.fileName)
|
|
|
|
|
print(letter.fileBase64)
|
|
|
|
|
print(letter.sign)
|
|
|
|
|
print(letter.encryptType)
|
|
|
|
|
print(letter.encryptKey)
|
|
|
|
@ -48,8 +64,24 @@ def main():
|
|
|
|
|
return letter # 方便recv测试,以后可以删除。
|
|
|
|
|
pass
|
|
|
|
|
def selectFile() -> str:
|
|
|
|
|
# s = "public.pem"
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
s = input("输入文件路径:")
|
|
|
|
|
with open(s, "rb") as _:
|
|
|
|
|
pass
|
|
|
|
|
return s
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print("错误: 文件未找到,请输入正确的文件路径。")
|
|
|
|
|
except PermissionError:
|
|
|
|
|
print("错误: 无法访问该文件,请检查权限。")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"发生未知错误: {e}")
|
|
|
|
|
|
|
|
|
|
# 获得文件名
|
|
|
|
|
def getFileName(fName:str) -> str:
|
|
|
|
|
filePath = os.path.split(fName)
|
|
|
|
|
return filePath[-1]
|
|
|
|
|
|
|
|
|
|
def sendLetter(letter: Letter, target="192.168.195.162:8426"):
|
|
|
|
|
# 向目标ip和端口发送指定的信件
|
|
|
|
@ -64,34 +96,35 @@ def selectSymEncryptionChoice():
|
|
|
|
|
|
|
|
|
|
# 选择加密算法
|
|
|
|
|
while True:
|
|
|
|
|
encryWay = input("Choose the way for encryption (aes/sm4): ").strip().lower() # 统一转成小写
|
|
|
|
|
encryWay = input("选择加密算法 (aes/sm4): ").strip().lower() # 统一转成小写
|
|
|
|
|
if encryWay in ["aes", "sm4"]:
|
|
|
|
|
letterWay = encryWay
|
|
|
|
|
print(f"You have selected '{encryWay}' encryption.")
|
|
|
|
|
print(f"已经选择 '{encryWay}'.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter 'aes' or 'sm4'.")
|
|
|
|
|
print("非法选择,请输入 'aes' 或 'sm4'.")
|
|
|
|
|
|
|
|
|
|
# 选择加密算法的模式
|
|
|
|
|
while True:
|
|
|
|
|
if encryWay == "aes":
|
|
|
|
|
encryMode = input("Choose the encryption mode (ecb/cbc/cfb/ofb): ").strip().lower()
|
|
|
|
|
encryMode = input("选择加密算法模式 (ecb/cbc/cfb/ofb): ").strip().lower()
|
|
|
|
|
if encryMode in ["ecb", "cbc", "cfb", "ofb"]:
|
|
|
|
|
letterMode = encryMode
|
|
|
|
|
print(f"You have selected '{encryMode}' encryption mode.")
|
|
|
|
|
print(f"已选择 '{encryMode}' 加密模式.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter ecb/cbc/cfb/ofb")
|
|
|
|
|
print("非法输入,请输入 ecb/cbc/cfb/ofb")
|
|
|
|
|
|
|
|
|
|
elif encryWay == "sm4":
|
|
|
|
|
encryMode = input("Choose the encryption mode (ecb/cbc): ").strip().lower()
|
|
|
|
|
encryMode = input("选择加密模式 (ecb/cbc): ").strip().lower()
|
|
|
|
|
if encryMode in ["ecb", "cbc"]:
|
|
|
|
|
letterMode = encryMode
|
|
|
|
|
print(f"You have selected '{encryMode}' encryption mode.")
|
|
|
|
|
print(f"已选择 '{encryMode}' 加密模式.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter ecb/cbc")
|
|
|
|
|
print("非法输入,请输入 enter ecb/cbc")
|
|
|
|
|
|
|
|
|
|
# 返回加密方法和加密方法的模式
|
|
|
|
|
return encryWay, encryMode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -128,6 +161,7 @@ def getSign(document_bytes):
|
|
|
|
|
|
|
|
|
|
return signDocuHash
|
|
|
|
|
|
|
|
|
|
# 获得加密的方法和模式,封装信封
|
|
|
|
|
def getEncryptType():
|
|
|
|
|
encryType = f"{letterWay}_{letterMode}".upper()
|
|
|
|
|
|
|
|
|
@ -161,5 +195,4 @@ if __name__ == "__main__":
|
|
|
|
|
#
|
|
|
|
|
# encryptType = f"{letterWay}_{letterMode}".upper()
|
|
|
|
|
# print(encryptType)
|
|
|
|
|
|
|
|
|
|
main()
|