#🔒 Problem writing serialised encrypted pem file to a .pem file

4 messages · Page 1 of 1 (latest)

trim kindle
#

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.

fresh micaBOT
#

@trim kindle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fresh micaBOT
#

@trim kindle

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.