本文介绍了如何从文件将公共RSA密钥加载到Python-RSA中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenSSL通过以下命令生成了私钥和公钥:

I generated a private and a public key using OpenSSL with the following commands:

openssl genrsa -out private_key.pem 512
openssl rsa -in private_key.pem -pubout -out public_key.pem

然后我尝试使用Python-RSA使用python脚本加载它们:

I then tried to load them with a python script using Python-RSA:

import os
import rsa

with open('private_key.pem') as privatefile:
    keydata = privatefile.read()
privkey = rsa.PrivateKey.load_pkcs1(keydata,'PEM')

with open('public_key.pem') as publicfile:
    pkeydata = publicfile.read()

pubkey = rsa.PublicKey.load_pkcs1(pkeydata)

random_text = os.urandom(8)

#Generate signature
signature = rsa.sign(random_text, privkey, 'MD5')
print signature

#Verify token
try:
    rsa.verify(random_text, signature, pubkey)
except:
    print "Verification failed"

我的python脚本尝试加载公钥时失败:

My python script fails when it tries to load the public key:

ValueError: No PEM start marker "-----BEGIN RSA PUBLIC KEY-----" found

推荐答案

Python-RSA使用PEM RSAPublicKey格式,而PEM RSAPublicKey格式使用页眉和页脚行: openssl注释

Python-RSA uses the PEM RSAPublicKey format and the PEM RSAPublicKey format uses the header and footer lines:openssl NOTES

-----BEGIN RSA PUBLIC KEY-----
-----END RSA PUBLIC KEY-----

以RSAPublicKey格式输出私钥的公有部分:openssl示例

Output the public part of a private key in RSAPublicKey format:openssl EXAMPLES

 openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem

这篇关于如何从文件将公共RSA密钥加载到Python-RSA中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:20