Python cryptography package RSA -- save private key to DB
<p>I want to encrypt something with RSA from python <code>cryptography</code> library. 
(<a href="https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/" rel="nofollow">https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/</a>)</p>

<p>First think first, I have my secret msg and two types of keys(public and private): </p>

<pre><code>from cryptography.hazmat.primitives.asymmetric import rsa 

SECRET = 'Ligula Venenatis Etiam Fermentum'

private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)

public_key = private_key.public_key() 
</code></pre>

<p>Now I can encrypt msg with public_key: </p>

<pre><code>from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

ciphertext = public_key.encrypt(
    SECERT,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)
</code></pre>

<p>Great! But due to decrypt this message I need to use <code>private_key</code>:</p>

<pre><code>plaintext = private_key.decrypt(
    ciphertext,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)
</code></pre>

<p>All works fine, the only problem is -- I need to save private key to database and decrypt msg later. Cant use RSA class instance for that purposes. </p>

<p>Maybe Im using wrong tool or just don't know this library well, but so far I'm not found answer in documentation. </p>

<p>Will appreciate any help :)</p>