|
|
|
@ -1,7 +1,12 @@
|
|
|
|
|
# 模式,文件,自己的公钥从哪里来,别人的公钥从哪里来
|
|
|
|
|
from entity.Letter import Letter
|
|
|
|
|
from tool.symmetric.AES import AESUtils
|
|
|
|
|
from tool.symmetric.SM4 import encrypt_ecb, decrypt_cbc_with_iv
|
|
|
|
|
from Crypto.Random import get_random_bytes
|
|
|
|
|
|
|
|
|
|
# 两个变量,记录信封
|
|
|
|
|
letterWay = ""
|
|
|
|
|
letterMode = ""
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
# 用户输入各种数据填充letter字段
|
|
|
|
@ -16,33 +21,40 @@ def sendLetter(letter: Letter, target="192.168.195.162:8426"):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 选择对称加密的方法和模式(aes/sm2)
|
|
|
|
|
# 选择对称加密的方法和模式(aes/sm4)
|
|
|
|
|
def selectSymEncryptionChoice():
|
|
|
|
|
global letterWay, letterMode
|
|
|
|
|
encryWay = ""
|
|
|
|
|
encryMode = ""
|
|
|
|
|
|
|
|
|
|
# 选择加密算法
|
|
|
|
|
while True:
|
|
|
|
|
encryWay = input("Choose the way for encryption (aes/sm2): ").strip().lower() # 统一转成小写
|
|
|
|
|
if encryWay in ["aes", "sm2"]:
|
|
|
|
|
encryWay = input("Choose the way for encryption (aes/sm4): ").strip().lower() # 统一转成小写
|
|
|
|
|
if encryWay in ["aes", "sm4"]:
|
|
|
|
|
letterWay = encryWay
|
|
|
|
|
print(f"You have selected '{encryWay}' encryption.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter 'aes' or 'sm2'.")
|
|
|
|
|
print("Invalid choice. Please enter 'aes' or 'sm4'.")
|
|
|
|
|
|
|
|
|
|
# 选择加密算法的模式
|
|
|
|
|
while True:
|
|
|
|
|
if encryWay == "aes":
|
|
|
|
|
encryMode = input("Choose the encryption mode (ecb/cbc/cfb/ofb): ").strip().lower()
|
|
|
|
|
if encryMode in ["ecb", "cbc", "cfb", "ofb"]:
|
|
|
|
|
letterMode = encryMode
|
|
|
|
|
print(f"You have selected '{encryMode}' encryption mode.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter ecb/cbc/cfb/ofb")
|
|
|
|
|
|
|
|
|
|
elif encryWay == "sm2":
|
|
|
|
|
print()
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
elif encryWay == "sm4":
|
|
|
|
|
encryMode = input("Choose the encryption mode (ecb/cbc): ").strip().lower()
|
|
|
|
|
if encryMode in ["ecb", "cbc"]:
|
|
|
|
|
print(f"You have selected '{encryMode}' encryption mode.")
|
|
|
|
|
break # 输入有效后退出循环
|
|
|
|
|
else:
|
|
|
|
|
print("Invalid choice. Please enter ecb/cbc")
|
|
|
|
|
|
|
|
|
|
return encryWay, encryMode
|
|
|
|
|
|
|
|
|
@ -54,12 +66,18 @@ def SymEncryption(encryData, key: bytes = None):
|
|
|
|
|
|
|
|
|
|
if way == "aes":
|
|
|
|
|
aesUtils = AESUtils(key)
|
|
|
|
|
# tureKey 为 真实返回的Key,如果没有设置初始值,那么key是随机生成
|
|
|
|
|
encryptedData, tureKey = aesUtils.encrypt(encryData, mode=mode) # 这里encryData要改为文件内容
|
|
|
|
|
return encryptedData,tureKey
|
|
|
|
|
encryptedData, trueKey = aesUtils.encrypt(encryData, mode=mode) # 这里encryData要改为文件内容
|
|
|
|
|
return encryptedData,trueKey
|
|
|
|
|
|
|
|
|
|
if way == "sm4":
|
|
|
|
|
key = get_random_bytes(16)
|
|
|
|
|
if mode == "ecb":
|
|
|
|
|
encrypted_data = encrypt_ecb(data, key)
|
|
|
|
|
# mode 为 cbc
|
|
|
|
|
else:
|
|
|
|
|
encrypted_data = decrypt_cbc_with_iv(data, key)
|
|
|
|
|
|
|
|
|
|
if way == "sm2":
|
|
|
|
|
pass
|
|
|
|
|
return encrypted_data, key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getKey():
|
|
|
|
@ -69,9 +87,10 @@ def getKey():
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# 示例数据和密钥
|
|
|
|
|
data = "Hello, AES!"
|
|
|
|
|
# key = "1234567890abcdef"
|
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
|
|
data,key = SymEncryption(data)
|
|
|
|
|
data, akey = SymEncryption(data)
|
|
|
|
|
|
|
|
|
|
print(data,akey)
|
|
|
|
|
|
|
|
|
|
print(data,key)
|
|
|
|
|
encryptType = f"{letterWay}_{letterMode}".upper()
|
|
|
|
|
print(encryptType)
|