在Python中,可以使用`cryptography`库或`rsa`库来解密RSA加密的信息。以下是使用`cryptography`库进行RSA解密的示例代码:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
加载私钥
with open('private_key.pem', 'rb') as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
加载公钥(如果需要)
with open('public_key.pem', 'rb') as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
加密后的消息
encrypted_message = b'加密后的内容'
使用私钥解密
decrypted_message = private_key.decrypt(
encrypted_message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(decrypted_message.decode('utf-8')) 输出解密后的消息
请确保你有对应的私钥文件(`private_key.pem`),并且该文件包含私钥信息。如果你需要使用公钥进行加密和解密,请确保你有对应的公钥文件(`public_key.pem`)。