load_pub_key 中的文件访问

发布于 2024-08-03 01:00:04 字数 542 浏览 8 评论 0原文

考虑以下代码:

fileHandle = open ( 'test8.pem','w' )
fileHandle.write (data)
pub_key = M2Crypto.RSA.load_pub_key(open('test8.pem'))

这会产生以下错误:

 File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 343, in load_pub_key
bio = BIO.openfile(file) 
  File "/usr/lib/python2.4/site-packages/M2Crypto/BIO.py", line 186, in openfile
    return File(open(filename, mode))
IOError: [Errno 2] No such file or directory: ''

如何将文件传递到 load_pub_key 方法中,以便只需传递文件名即可访问它?

Consider the following code:

fileHandle = open ( 'test8.pem','w' )
fileHandle.write (data)
pub_key = M2Crypto.RSA.load_pub_key(open('test8.pem'))

Which produces the following error:

 File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 343, in load_pub_key
bio = BIO.openfile(file) 
  File "/usr/lib/python2.4/site-packages/M2Crypto/BIO.py", line 186, in openfile
    return File(open(filename, mode))
IOError: [Errno 2] No such file or directory: ''

How do I pass the file into load_pub_key method so it can be accessible by simply passing the file name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-08-10 01:00:04

如果您传递不带引号的 test8.pem,Python 会将其解释为未定义的变量名称,因此会出现错误。

我不知道您正在使用的具体库,但我猜您需要传递 fileHandle 。

If you pass test8.pem without quotes, Python interprets it as the name of a variable, which is not defined, hence the error.

I don't know the specific library you are using but I would guess that you need to pass fileHandle instead.

戴着白色围巾的女孩 2024-08-10 01:00:04

这应该适合你:

fname = 'test8.pem'
fileHandle = open(fname, 'w')
fileHandle.write(data)
fileHandle.close()
pub_key = M2Crypto.RSA.load_pub_key(fname)

this should work for you:

fname = 'test8.pem'
fileHandle = open(fname, 'w')
fileHandle.write(data)
fileHandle.close()
pub_key = M2Crypto.RSA.load_pub_key(fname)
无声静候 2024-08-10 01:00:04

我也有同样的问题。我尝试加载文件处理程序而不是路径,但没有帮助。

锻炼的是使用 M2Crypto 的 X509 模块。您可以尝试使用此函数来获取公钥实例:

certificate = M2Crypto.X509.load_cert(cert_path)
pubkey = certificate.get_pubkey()

更多详细信息请参阅以下答案:
使用 m2crypto 验证 RSACryptoServiceProvider 消息签名

I also have the same issue. I tried loading a file handler instead of path but it didn't help.

The thing that workout was using X509 module from M2Crypto. You can try use this functions to obtain a public key instance:

certificate = M2Crypto.X509.load_cert(cert_path)
pubkey = certificate.get_pubkey()

More details in the following answer:
RSACryptoServiceProvider message signature verification with m2crypto

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文