添加rsa函数,oaep填充

main
周平 10 months ago
parent 171f891d63
commit 103ee99d26

@ -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)

@ -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)])
@ -59,3 +63,24 @@ def get_identify_code():
code = hashlib.new('md5', s.encode("utf8")).hexdigest()
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

Loading…
Cancel
Save