用Python加密 [英] Encryption with Python
问题描述
我在Python中使用加密函数,我想使用公钥加密随机数。
I'm making an encryption function in Python and I want to encrypt a random number using a public key.
我想知道如果我使用Crypto包( Crypto.publicKey.pubkey
)比我如何使用像...的方法。
I wish to know that if I use Crypto package (Crypto.publicKey.pubkey
) than how can I use the method like...
def encrypt(self,plaintext,k)
> k 本身是一个随机数,就是这个意思的关键。
Here the k
is itself a random number, is this mean the key. Can somebody help me with somewhat related?
推荐答案
您尝试使用公钥加密对话加密的会话/消息密钥的收件人?
Are you trying to encrypt a session/message key for symmetric encryption using the public key of the recipient? It might be more straightforward to use, say, SSH or TLS in those cases.
返回您的问题:
Me Too Crypto (M2Crypto)是关于openssl的一个很好的包装。
Me Too Crypto (M2Crypto) is a nice wrapper around openssl.
首先,您需要获取收件人的公钥:
First, you need to get the public key of the recipient:
recip = M2Crypto.RSA.load_pub_key(open('recipient_public_key.pem','rb').read())
现在您可以加密您的邮件:
Now you can encrypt your message:
plaintext = random_integer_you_want_to_encrypt
msg = recip.public_encrypt(plaintext,RSA.pkcs1_padding)
现在只有拥有收件人解密。
Now only someone with the private key of the recipient can decrypt it.
这篇关于用Python加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!