简体   繁体   中英

Python | How to import private key as string from .KEY file

I've trying to do this for a couple of days, I don't seem to find anything that solves my problem.

What I know is that I have a .key file, which is essentially a .pem file. It should be something like this at the end:

---BEGIN PRIVATE KEY---
M....
.....
.....
---END PRIVATE KEY---

This file requires a password. If I open the file directly I would just get bytes that I cannot read.

My goal is to open this file, with the password, in order to get a string in the PEM format. I've tried using:

from cryptography.hazmat.primitives.serialization import load_pem_private_key

key = load_pem_private_key(file.read(), password=password.encode(), backend=default_backend())

But I get ValueError, it says "Could not deserialize key data" I'm not sure what's the problem

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM