From 103ee99d2654684571e115c2239b15836f7d990a Mon Sep 17 00:00:00 2001 From: zhouping Date: Mon, 8 Jul 2024 09:42:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0rsa=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8Coaep=E5=A1=AB=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- website/util/rsa_OAEP.py | 75 ++++++++++++++++++++++++++++++++++++++++ website/util/sysinfo.py | 33 +++++++++++++++--- 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 website/util/rsa_OAEP.py diff --git a/website/util/rsa_OAEP.py b/website/util/rsa_OAEP.py new file mode 100644 index 0000000..96da94c --- /dev/null +++ b/website/util/rsa_OAEP.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +from Crypto.PublicKey import RSA +from Crypto.Cipher import PKCS1_OAEP +import base64 + +""" +RSA 加密中,有两种常见的填充方式:PKCS1_v1_5 和 PKCS1_OAEP。这两种填充方式在安全性和性能方面都有一些差异。 + +PKCS1_v1_5 填充方式: + 这是较早的 RSA 填充方式,相对简单且性能较好。 + 但是它存在一些安全隐患,比如可能会受到选择密文攻击(Chosen Ciphertext Attack, CCA)。 +PKCS1_OAEP 填充方式: + PKCS1_OAEP 是一种更加安全的填充方式,它使用了随机填充来提高安全性。 + PKCS1_OAEP 可以抵御选择密文攻击(CCA)和其他一些攻击方式,因此被认为更加安全。 + 但是,PKCS1_OAEP 的性能略低于 PKCS1_v1_5,因为它需要进行更多的计算。 +""" + +# 生成密钥对 +def generate_keys(): + key = RSA.generate(2048) + private_key = key.export_key() + public_key = key.publickey().export_key() + return private_key, public_key + +# 加密消息,message为bytes类型 +def encrypt_message(public_key, message): + cipher = PKCS1_OAEP.new(RSA.import_key(public_key)) + encrypted_message = base64.b64encode(cipher.encrypt(message)) + print("Encrypted message:", encrypted_message.decode()) + + return encrypted_message + +# 解密消息 +def decrypt_message(private_key, encrypted_message): + decipher = PKCS1_OAEP.new(RSA.import_key(private_key)) + decrypted_message = decipher.decrypt(base64.b64decode(encrypted_message)) + print("Decrypted message:", decrypted_message.decode()) + return decrypted_message.decode() + +# 主程序 +if __name__ == "__main__": + # 生成密钥对 + private_key, public_key = generate_keys() + print(private_key) + print(public_key) + + # 序列化公钥和私钥 + # private_pem = private_key.private_bytes( + # encoding=serialization.Encoding.PEM, + # format=serialization.PrivateFormat.PKCS8, + # encryption_algorithm=serialization.NoEncryption() + # ) + # public_pem = public_key.public_bytes( + # encoding=serialization.Encoding.PEM, + # format=serialization.PublicFormat.SubjectPublicKeyInfo + # ) + + # 打印公钥和私钥 + print("Private Key:") + print(private_key.decode()) + print("Public Key:") + print(public_key.decode()) + + # 待加密消息 + message = b"Hello, RSA!" + + # 加密消息 + encrypted_message = encrypt_message(public_key, message) + print("Encrypted Message:") + print(encrypted_message) + + # 解密消息 + decrypted_message = decrypt_message(private_key, encrypted_message) + print("Decrypted Message:") + print(decrypted_message) diff --git a/website/util/sysinfo.py b/website/util/sysinfo.py index e79a7c2..eb418f7 100644 --- a/website/util/sysinfo.py +++ b/website/util/sysinfo.py @@ -1,8 +1,10 @@ -import subprocess -import socket import hashlib -import uuid import logging +import os +import socket +import subprocess +import uuid + def get_cpu_id(): p = subprocess.Popen(["dmidecode -t 4 | grep ID"], @@ -20,6 +22,7 @@ def get_cpu_id(): break return lines + def get_board_serialnumber(): p = subprocess.Popen(["dmidecode -t 2 | grep Serial"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -36,6 +39,7 @@ def get_board_serialnumber(): break return lines + def get_identify_code(): mac = uuid.UUID(int=uuid.getnode()).hex[-12:] mac_addr = ":".join([mac[e:e + 2] for e in range(0, 11, 2)]) @@ -58,4 +62,25 @@ def get_identify_code(): logging.info(s) code = hashlib.new('md5', s.encode("utf8")).hexdigest() - return code \ No newline at end of file + return code + + +def get_system_uuid(): + # 获取系统uuid + # 这个 UUID 是与硬件相关的,因此即使在 Docker 容器中,它也应该是唯一的,可以用来标识宿主机,而不是容器本身。 + with open("/sys/class/dmi/id/product_uuid", "r") as f: + host_uuid = f.read().strip() + return host_uuid + + +def get_docker_container_id(): + # 获取当前 Docker 容器的 ID + cmd = "cat /proc/self/cgroup" + output = os.popen(cmd) + rests = output.readlines() + container_message = rests[-1] + if not container_message: + container_id = "abc" + else: + container_id = container_message.strip().split("docker/")[-1] + return container_id