加载私钥失败,OpenSSL.crypto.Error: [] [英] Loading private key fails with OpenSSL.crypto.Error: []

查看:85
本文介绍了加载私钥失败,OpenSSL.crypto.Error: []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenSSL 加载私钥:

I'm trying to load a private key using OpenSSL with:

from OpenSSL import crypto

PRIVATE_KEY = 'private_key.pem'
with open(PRIVATE_KEY, 'rb') as fh:
    private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, fh.read(), '')

但是我收到了这个无用的错误:

But I'm receiving this unhelpful error:

Traceback (most recent call last):
  File "keytest.py", line 5, in <module>
    private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, fh.read(), '')
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/crypto.py", line 2010, in load_privatekey
    _raise_current_error()
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/_util.py", line 22, in exception_from_error_queue
    raise exceptionType(errors)
OpenSSL.crypto.Error: []

我能找到的有关此错误的唯一参考是Twisted Python、TLS 和客户端/服务器证书身份验证错误.然而,作者无意中尝试使用 twisted.internet.ssl.PrivateCertificate.loadPEM()(最终是 OpenSSL.crypto.load_privatekey()) 而不是 twisted.internet.ssl.Certificate.loadPEM()(最终是 OpenSSL.crypto.load_certificate()).

The only reference I can find to this error is Twisted Python, TLS and client/server certificate authentication error. However, the author was accidentally trying load a public certificate as a private key with twisted.internet.ssl.PrivateCertificate.loadPEM() (ultimately OpenSSL.crypto.load_privatekey()) instead of twisted.internet.ssl.Certificate.loadPEM() (ultimately OpenSSL.crypto.load_certificate()).

什么可能导致这种情况?

What could cause this?

推荐答案

加载私钥导致错误的情况至少有两种:

There are at least two cases where loading a private key results in the error:

OpenSSL.crypto.Error: []

1) 如果私钥已加密,但您不希望它被加密.即,私钥包含:

1) If the private key is encrypted, but you were not expecting it to be encrypted. I.e., the private key contains:

-----BEGIN ENCRYPTED PRIVATE KEY-----
...
-----END ENCRYPTED PRIVATE KEY-----

代替:

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

2) 如果私钥已加密,但您提供的密码错误.

2) If the private key is encrypted, but you are providing the wrong password.

这篇关于加载私钥失败,OpenSSL.crypto.Error: []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆