代码拉取完成,页面将自动刷新
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
def generate_rsa_keys():
# private_key = rsa.generate_private_key(
# public_exponent=65537,
# key_size=2048,
# backend=default_backend()
# )
# public_key = private_key.public_key()
#
# # 序列化公钥和私钥
# pem = public_key.public_bytes(
# encoding=serialization.Encoding.PEM,
# format=serialization.PublicFormat.SubjectPublicKeyInfo
# )
# pem_private = private_key.private_bytes(
# encoding=serialization.Encoding.PEM,
# format=serialization.PrivateFormat.TraditionalOpenSSL,
# encryption_algorithm=serialization.NoEncryption()
# )
pem = b'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA02zpy+F92JitiI0oaeSU\njV+P7WrYwH3lbylD5Q20DKe/7wOsL/lawq2/Z9RWjgg+K51SC92JkXunybGv/aqf\nckx29P9BG9A+OhW9pPFdd25voCQgE8htL/I5IdEIGyS8iTBzmnUICcDCT59BPC2F\nlT0UlErbaLFKvr9RqTmPp8EcdWBh0gwQcBqhiojVwRndzbEKXpsCqIWgcEGxQaYz\nQEZcOiU7B94os822347K1aWJBHQeAhAtQ4a1BCBkEaFasAny3h7hBit1p3N3BMUx\nhdbiESBFoI4QVM+hOOnVQ7Na7KM+N/Rk2HsFlMzZJdYiaDf45WvLvlWgJknE3zXb\nsQIDAQAB\n-----END PUBLIC KEY-----\n'
# print(pem)
pem_private = b'-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA02zpy+F92JitiI0oaeSUjV+P7WrYwH3lbylD5Q20DKe/7wOs\nL/lawq2/Z9RWjgg+K51SC92JkXunybGv/aqfckx29P9BG9A+OhW9pPFdd25voCQg\nE8htL/I5IdEIGyS8iTBzmnUICcDCT59BPC2FlT0UlErbaLFKvr9RqTmPp8EcdWBh\n0gwQcBqhiojVwRndz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kdUYIyYYOAnS0lo558\nCayk428yA8dMTvF1jmvXJ9vEvZFEkMkEAhvQk0+0zxmY0deH1/biReA9kz/GcoGU\ngw4EBOS3fBlIu6mqsJjkalMkOrPpZaeH6D4UO8kI1417Zr0tpFsmijUzU6emcn0v\nc/RYBG0CgYEA4/H4LILR3cmehLm3O10ukvXjCc1ED34bLQf1WGTPEjM2bL2faXB2\nFJSXMsssrMFoAHrtrMa8TPGov3SshIqHrj9OCJ/S1GIwseUXOJl/hSECmnuyCe7K\n7FKaBPy21+vPeu2QWSZNiNR3vEuLaXfEa4MDmfaBTwAs7AhNuD4MLIc=\n-----END RSA PRIVATE KEY-----\n'
# print(pem_private)
print("*"*90)
return pem, pem_private
def rsa_encrypt(plaintext, public_key_pem):
public_key = serialization.load_pem_public_key(
public_key_pem,
backend=default_backend()
)
encrypted = public_key.encrypt(
plaintext.encode(),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted
def rsa_decrypt(ciphertext, private_key_pem):
private_key = serialization.load_pem_private_key(
private_key_pem,
password=None,
backend=default_backend()
)
decrypted = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return decrypted.decode()
# 使用示例
public_key_pem, private_key_pem = generate_rsa_keys()
plaintext = "Hello, RSA!"
ciphertext = rsa_encrypt(plaintext, public_key_pem)
print(ciphertext)
jiemi = rsa_decrypt(ciphertext, private_key_pem)
print(jiemi)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。