encryption - Python cryptography package RSA -- save private key to DB -


i want encrypt rsa python cryptography library. (https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/)

first think first, have secret msg , 2 types of keys(public , private):

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

now can encrypt msg public_key:

from cryptography.hazmat.primitives import hashes cryptography.hazmat.primitives.asymmetric import padding  ciphertext = public_key.encrypt(     secert,     padding.oaep(         mgf=padding.mgf1(algorithm=hashes.sha1()),         algorithm=hashes.sha1(),         label=none     ) ) 

great! due decrypt message need use private_key:

plaintext = private_key.decrypt(     ciphertext,     padding.oaep(         mgf=padding.mgf1(algorithm=hashes.sha1()),         algorithm=hashes.sha1(),         label=none     ) ) 

all works fine, problem -- need save private key database , decrypt msg later. cant use rsa class instance purposes.

maybe im using wrong tool or don't know library well, far i'm not found answer in documentation.

will appreciate :)

you can serialize private key without encryption.

pem = private_key.private_bytes(     encoding=serialization.encoding.pem,     format=serialization.privateformat.traditionalopenssl,     encryption_algorithm=serialization.noencryption() ) pem_data = pem.splitlines()[0] 

store pem_data database, , reload private key pem while need it.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -