python 如何使用密钥加密字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1320671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to encrypt a string using the key
提问by jfs
I have a 'public key' in a variable named varkey, for getting the public key I used the urllib and stored that public key in a variable. Now I want to encrypt a msg/string using the public key.
我在名为 varkey 的变量中有一个“公钥”,为了获取公钥,我使用了 urllib 并将该公钥存储在一个变量中。现在我想使用公钥加密一个 msg/string。
It's ok if somebody could lead me to some library.
如果有人能带我去某个图书馆也没关系。
回答by thsutton
My blog post (the passingcuriosity.com link in John Boker's answer) does AES -- a symmetric encryption algorithm -- using the M2Crypto library. M2Crypto is a Python wrapper around OpenSSL. The API is pretty much a straight translation of OpenSSL's into Python, so the somewhat sketchy documentation shouldn't be too much of a problem. If the public key encryption algorithm you need to use is supported by M2Crypto, then you could very well use it to do your public key cryptography.
我的博客文章(John Boker 回答中的 passcuriosity.com 链接)使用 M2Crypto 库执行 AES(一种对称加密算法)。M2Crypto 是一个围绕 OpenSSL 的 Python 包装器。该 API 几乎是 OpenSSL 到 Python 的直接翻译,因此有些粗略的文档应该不会有太大问题。如果 M2Crypto 支持您需要使用的公钥加密算法,那么您可以很好地使用它来进行您的公钥加密。
I found the M2Crypto test suiteto be a useful example of using its API. In particular, the RSA (in test_rsa.py), PGP (in test_pgp.py), and EVP (in test_evp.py) tests will help you figure out how to set up and use the library. Do be aware that they are unit-tests, so it can be a little tricky to figure out exactly what code is necessary and what is an artefact of being a test.
我发现M2Crypto 测试套件是使用其 API 的一个有用示例。特别是 RSA(在 test_rsa.py 中)、PGP(在 test_pgp.py 中)和 EVP(在 test_evp.py 中)测试将帮助您弄清楚如何设置和使用该库。请注意,它们是单元测试,因此要确切地弄清楚哪些代码是必要的以及什么是测试的人工制品可能有点棘手。
PS: As I'm new, my posts can only contain one link so I had to delete most of them. Sorry.
PS:因为我是新人,我的帖子只能包含一个链接,所以我不得不删除大部分。对不起。
Example
例子
from M2Crypto import RSA
rsa = RSA.load_pub_key('rsa.pub.pem')
encrypted = rsa.public_encrypt('your message', RSA.pkcs1_oaep_padding)
print encrypted.encode('base64')
Output
输出
X3iTasRwRdW0qPRQBXiKN5zvPa3LBiCDnA3HLH172wXTEr4LNq2Kl32PCcXpIMxh7j9CmysLyWu5 GLQ18rUNqi9ydG4ihwz3v3xeNMG9O3/Oc1XsHqqIRI8MrCWTTEbAWaXFX1YVulVLaVy0elODECKV 4e9gCN+5dx/aG9LtPOE=
回答by jfs
Here's the script that demonstrates how to encrypt a message using M2Crypto ($ easy_install m2crypto
) given that public key is in varkey
variable:
这是演示如何使用 M2Crypto ( $ easy_install m2crypto
)加密消息的脚本,因为公钥是varkey
可变的:
#!/usr/bin/env python
import urllib2
from M2Crypto import BIO, RSA
def readkey(filename):
try:
key = open(filename).read()
except IOError:
key = urllib2.urlopen(
'http://svn.osafoundation.org/m2crypto/trunk/tests/' + filename
).read()
open(filename, 'w').write(key)
return key
def test():
message = 'disregard the -man- (I mean file) behind curtain'
varkey = readkey('rsa.pub.pem')
# demonstrate how to load key from a string
bio = BIO.MemoryBuffer(varkey)
rsa = RSA.load_pub_key_bio(bio)
# encrypt
encrypted = rsa.public_encrypt(message, RSA.pkcs1_oaep_padding)
print encrypted.encode('base64')
del rsa, bio
# decrypt
read_password = lambda *args: 'qwerty'
rsa = RSA.load_key_string(readkey('rsa.priv2.pem'), read_password)
decrypted = rsa.private_decrypt(encrypted, RSA.pkcs1_oaep_padding)
print decrypted
assert message == decrypted
if __name__ == "__main__":
test()
Output
输出
gyLD3B6jXspHu+o7M/TGLAqALihw7183E2effp9ALYfu8azYEPwMpjbw9nVSwJ4VvX3TBa4V0HAU n6x3xslvOnegv8dv3MestEcTH9b3r2U1rsKJc1buouuc+MR77Powj9JOdizQPT22HQ2VpEAKFMK+ 8zHbkJkgh4K5XUejmIk= disregard the -man- (I mean file) behind curtain
回答by PinkTriangles
From my recent python experience, python doesn't do encryption natively. You need to use an external (3rd party) package. Each of these, obviously, offers a different experience. Which are you using? This will probably determine how your syntax will vary.
根据我最近的 python 经验,python 本身不进行加密。您需要使用外部(第 3 方)软件包。显然,这些都提供了不同的体验。你在用哪个?这可能会决定您的语法将如何变化。
回答by John Boker
回答by sorokin.in.ua
Have you ever heard about "RSAError: data too large for key size"?
您是否听说过“RSAError:数据对于密钥大小来说太大了”?
Try your sample with more long message:
使用更长的消息尝试您的示例:
encrypted = rsa.public_encrypt('My blog post (the passingcuriosity.com link in John Boker's answer) does AES -- a symmetric encryption algorithm -- using the M2Crypto library', RSA.pkcs1_oaep_padding)
回答by Gabriel Hurley
You could use MD5 or SHA1 hashing along with your key...
您可以将 MD5 或 SHA1 散列与您的密钥一起使用...