serialization.load_pem_private_key function always throws the value error could not deserialize data with python 3.6. It works well with python 2.7
Can you give us more details? What version of cryptography? How can we reproduce this? Can you give us a full stack trace?
Hi alex
I am using cryptography==2.7 with python 3.6.8
My code is
with open(creds.SIGNING_PEM_FILE_PATH, 'rb') as key_file:
private_key = serialization.load_pem_private_key(
(key_file.read()),
password=None,
backend=default_backend())
The error i am getting is :
File "/opt/python/cryptography/hazmat/backends/openssl/backend.py", line 1335, in _handle_key_loading_error
raise ValueError("Could not deserialize key data.")
I have tried multiple online solutions but none is working.
Thank you

For the complete traceback please have a look
Thank you
I also came this problem. maybe problom is that here ?
def _handle_key_loading_error(self):
errors = self._consume_errors()
if not errors:
raise ValueError("Could not deserialize key data.")
Hi Hondsome,
So did you solve this problem?
Thank you
Hi Hondsome,
So did you solve this problem?Thank you
No, I am trying
Hi Hondsome,
So did you solve this problem?Thank you
i solve that , uninstall cryptography 2.6 and install cryptography==2.2 . it worked
Thanks a lot Hondsome. Just to confirm python version is 3 right?
hhh,no . i use 2.7 . maybe is cryptography version problem , just try it. i solve my problem
@alex the Problem is coming up on AWS lambda but is working fine with python3.6 on local ubuntu system. Can you help us a bit here, we are stuck on this problem for quite a few days now.
P.S I'm @shaaaarpy 's colleague.
@alex the Problem is coming up on AWS lambda but is working fine with python3.6 on local ubuntu system. Can you help us a bit here, we are stuck on this problem for quite a few days now.
P.S I'm @shaaaarpy 's colleague.
Did you ever resolve it? I am facing the same issue. What was the solution?
Without clear instructions to reproduce this issue, there's not much we can do.
Different versions of OpenSSL can be picky about key formats. If you're seeing it work in one env but not another take a hard look at the key and make sure it's a valid PEM: https://cryptography.io/en/latest/faq/#why-can-t-i-import-my-pem-file
I'm going to close this since it's too generic but if you have a reproducer or can provide a sample key (that isn't used in your system!!!) that demonstrates the problem we can take a look. Please open a new issue for it :)
I have the same problem, but I can't even solve it with the change of the library version. Anyone have any ideas?
Most helpful comment
i solve that , uninstall cryptography 2.6 and install cryptography==2.2 . it worked