I am converting a regular pem file into an encrypted pem file using cryptography.hazmat.primitives.serialization.pkcs12 module with the below function and encryption algorithm
from cryptography.hazmat.primitives.serialization import pkcs12
pkcs12.serialize_key_and_certificates(...)
function using the encryption algorithm
encryption_algorithm = (
PrivateFormat.PKCS12.encryption_builder()
.kdf_rounds(3)
.key_cert_algorithm(pkcs12.PBES.PBESv1SHA1And3KeyTripleDESCBC)
.hmac_hash(hashes.SHA256())
.build(password.encode(ENCODING))
)
I am able to get a byte array of encrypted data. I'd like to write it to a file as an encrypted pem file with the headers
-----BEGIN ENCRYPTED PRIVATE KEY-----
data
-----END ENCRYPTED PRIVATE KEY-----
however, I couldn't find a function or method that could write it in that format. I am not looking for a .p12 file in binary format but a .pem file itself with the -----BEGIN ENCRYPTED PRIVATE KEY-----.
Any pointers is appreciated. Thanks.