PyJWT,需要 PEM 格式的密钥
Posted
技术标签:
【中文标题】PyJWT,需要 PEM 格式的密钥【英文标题】:PyJWT, Expecting a PEM-formatted key 【发布时间】:2015-08-17 00:06:37 【问题描述】:我已经从 Layer 复制粘贴了这段代码:
https://github.com/layerhq/support/blob/master/identity-services-samples/python/controller.py
有人告诉我另外两个人在 mac 机器上成功地运行了它。我使用的是 Windows 7,我收到 TypeError: Expecting a PEM-formatted key when running code:
#Read RSA key
root = os.path.dirname("__file__")
with open(os.path.join(root, RSA_KEY_PATH), 'r') as rsa_priv_file:
#Not sure about adding the utf-8 AT ALL
priv_rsakey = RSA.importKey(rsa_priv_file.read())
#Create identity token
#Make sure you have PyJWT and PyCrypto libraries installed and imported
identityToken = jwt.encode(
payload=
"iss": PROVIDER_ID, # String - The Provider ID found in the Layer Dashboard
"prn": user_id, # String - Provider's internal ID for the authenticating user
"iat": datetime.datetime.now(), # Integer - Time of Token Issuance in RFC 3339 seconds
"exp": datetime.datetime.utcnow() + datetime.timedelta(seconds=30), # Integer - Arbitrary Token Expiration in RFC 3339 seconds
"nce": nonce # The nonce obtained via the Layer client SDK.
,
key=priv_rsakey,
headers =
"typ": "JWS", # String - Expresses a MIME Type of application/JWS
"alg": "RS256", # String - Expresses the type of algorithm used to sign the token, must be RS256
"cty": "layer-eit;v=1", # String - Express a Content Type of Layer External Identity Token, version 1
"kid": KEY_ID # String - Private Key associated with "layer.pem", found in the Layer Dashboard
,
algorithm="RS256"
)
print identityToken
我不知道我的PEM格式的密钥应该如何,但如下(我在发布之前手动更改了密钥):
-----开始 RSA 私钥----- MIICWwIBAAKBgQCWjLl4Hw8PLto2wGGxrpJ0afHQDX2iJ/xqNXyybgG0GxZI344u HXxn/bRzH4Z39pw3vGKrvd21hyln5/qaJGoCSb7QHAtdz8pfNF3t96DBcDh2aDMe +8W/4mZqiDi2zNqxAnmuv6vSKnU3UyqFpuPCdXFiA8Gt8OqHHLks8MYYjwIDAQAB AoGAII9i9YXSHfiGHV1SQ7l7102qjNK+HzoCr33N8FE2cRuCoFQpTfP2mg8W3ect j/h4JN3LgVVZBA+tdtiptHi3rF1W4yNs4pO3xYLJZHoCdG5CjHoMzEf2VwixK6+f 8FCCNErG8yxCpcPDH8D0NjMFzJ0t1kwsBQ0w5iSvdY3qtLECQQDgQPVk6W79kNS9 gBoZAB71yJazQpCwJ2SLYj6aFNZDX1Jpg5GRoVOs59gl13UF+9AwJf5MCIMbqaly T1/4aBa1AkEAq9y0DwSjNLl26XeBhWycQJZUu5lsJVqJcx2q0GsKGx5Ntei/48bD ldi7ZXWdA6o4OOV2GpW1hX7vssPHBXBYswJAMGXUkDLRAWiAgWVkPIEKLYFdBNMY 5uuZh3tsh1R36ZJo9tyuTf/DT026GHCnkOMHy1xnxYEQyqeTKlh3HeVZAQJAIQYC r1QTzEDLAaeOAsZQ6/Q3Cek4545TBgSwWuYGhhl3kznLDYiW8oc30xIbfqXJsd0d qZQU0jcG9hG6f2gMEwJALHz0jXhpjo+UygUjdKbW/mfKRirSYiGLCYQHiNc4aaDe omr9XLu9Hc6aI9RVGIFMnygaI9Ahu2mNkpj5UoaTTA==
-----结束 RSA 私钥-----
我找不到任何有效的解决方案。 谁有想法?谢谢
【问题讨论】:
您使用的是哪个版本的 Python? Python 2.7;在 shell_plus 中运行 --notebook, jupyter This question 表示-----BEGIN PRIVATE KEY-----
是预期的格式;你有不同的格式。尝试转换它(我猜 OpenSSL 会有这个工具)。
谢谢,但在同一个问题中,据说 BEGIN RSA PRIVATE KEY 也是可以接受的。我也试过另一个,没有结果。这适用于我被告知的 Mac。
仅供参考,既然您已经公开了该密钥,您将永远无法再使用它!
【参考方案1】:
变化:
priv_rsakey = RSA.importKey(rsa_priv_file.read())
到:
priv_rsakey = rsa_priv_file.read()
【讨论】:
我认为删除文件页眉和页脚会更安全,因为已知的文件头可能有助于破解密钥:priv_rsakey = rsa_priv_file.read().replace("-----BEGIN RSA PRIVATE KEY-----\n", "").replace("-----END RSA PRIVATE KEY-----\n", "")
以上是关于PyJWT,需要 PEM 格式的密钥的主要内容,如果未能解决你的问题,请参考以下文章