首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PyCrypto中使用X509证书?

如何在PyCrypto中使用X509证书?
EN

Stack Overflow用户
提问于 2012-10-16 17:18:48
回答 1查看 33K关注 0票数 22

我想用PyCrypto在python中加密一些数据。

但是,在使用key = RSA.importKey(pubkey)时出现错误

代码语言:javascript
运行
复制
RSA key format is not supported

密钥是使用以下命令生成的:

代码语言:javascript
运行
复制
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.pem

代码是:

代码语言:javascript
运行
复制
def encrypt(data):
    pubkey = open('mycert.pem').read()
    key = RSA.importKey(pubkey)
    cipher = PKCS1_OAEP.new(key)
    return cipher.encrypt(data)
EN

回答 1

Stack Overflow用户

发布于 2017-01-10 03:36:59

这里有一个很好的例子:https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.Cipher.PKCS1_OAEP-module.html

代码语言:javascript
运行
复制
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA

# sender side
message = 'To be encrypted'
key = RSA.importKey(open('pubkey.der').read())
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(message)

# receiver side
key = RSA.importKey(open('privkey.der').read())
cipher = PKCS1_OAP.new(key)
message = cipher.decrypt(ciphertext)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12911373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档