0

My current code to generate an OpenSSL public-private key pair:

import OpenSSL

key = OpenSSL.crypto.PKey()
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
public_key = OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, key)
private_key = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)
print(public_key, private_key, sep="\n")

1 Answer 1

0

The documentation recommends that you call key.to_cryptography_key() to get a cryptographic key, and then use pyca/cryptography to perform the encryption.

https://www.pyopenssl.org/en/stable/api/crypto.html

Given your crypto key:

result = crypto_key.encrypt(b"Hello there")

I should add, RSA is primary intended for signatures and verification, and key exchange. It's way too slow for general encryption.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I do know that RSA is slow am using it to send symmetric keys across a server client.
Sorry then. Just making sure. When someone says "I want to encrypt using RSA", I need to both answer the question and make sure they understand what they're doing! But hopefully I did answer your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.