You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
FileSecureTransfer/sender/sender.py

77 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 模式,文件,自己的公钥从哪里来,别人的公钥从哪里来
from entity.Letter import Letter
from tool.symmetric.AES import AESUtils
def main():
# 用户输入各种数据填充letter字段
# 获取用户的公私钥对进行签名
# 使用对方的公钥进行加密
# 发送信件
pass
def sendLetter(letter: Letter, target="192.168.195.162:8426"):
# 向目标ip和端口发送指定的信件
pass
# 选择对称加密的方法和模式(aes/sm2)
def selectSymEncryptionChoice():
encryWay = ""
encryMode = ""
# 选择加密算法
while True:
encryWay = input("Choose the way for encryption (aes/sm2): ").strip().lower() # 统一转成小写
if encryWay in ["aes", "sm2"]:
print(f"You have selected '{encryWay}' encryption.")
break # 输入有效后退出循环
else:
print("Invalid choice. Please enter 'aes' or 'sm2'.")
# 选择加密算法的模式
while True:
if encryWay == "aes":
encryMode = input("Choose the encryption mode (ecb/cbc/cfb/ofb): ").strip().lower()
if encryMode in ["ecb", "cbc", "cfb", "ofb"]:
print(f"You have selected '{encryMode}' encryption mode.")
break # 输入有效后退出循环
else:
print("Invalid choice. Please enter ecb/cbc/cfb/ofb")
elif encryWay == "sm2":
print()
break # 输入有效后退出循环
return encryWay, encryMode
# 使用对称加密,返回加密后的数据和随机生成的密钥
def SymEncryption(encryData, key: bytes = None):
# 获得加密的方法和加密的模式
way, mode = selectSymEncryptionChoice()
if way == "aes":
aesUtils = AESUtils(key)
# tureKey 为 真实返回的Key如果没有设置初始值那么key是随机生成
encryptedData, tureKey = aesUtils.encrypt(encryData, mode=mode) # 这里encryData要改为文件内容
return encryptedData,tureKey
if way == "sm2":
pass
def getKey():
pass
if __name__ == "__main__":
# 示例数据和密钥
data = "Hello, AES!"
# key = "1234567890abcdef"
print(data)
data,key = SymEncryption(data)
print(data,key)