Python 使用 pycrypto,如何导入 RSA 公钥并使用它来加密字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21327491/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:36:52  来源:igfitidea点击:

Using pycrypto, how to import a RSA public key and use it to encrypt a string?

pythonrsapycrypto

提问by user21916

The RSA public key:

RSA 公钥:

pubkey = 'MIGfMA0GCSqGSIb3DQEBA3UAA4GNADCBiQKBgQC35eMaYoJXEoJt5HxarHkzDBEMU3qIWE0HSQ77CwP/8UbX07W2XKwngUyY4k6Hl2M/n9TOZMZsiBzer/fqV+QNPN1m9M94eUm2gQgwkoRj5battRCaNJK/23GGpCsTQatJN8PZBhJBb2Vlsvw5lFrSdMT1R7vaz+2EeNR/FitFXwIDAQAB'

PUBKEY = 'MIGfMA0GCSqGSIb3DQEBA3UAA4GNADCBiQKBgQC35eMaYoJXEoJt5HxarHkzDBEMU3qIWE0HSQ77CwP / 8UbX07W2XKwngUyY4k6Hl2M / n9TOZMZsiBzer / fqV + QNPN1m9M94eUm2gQgwkoRj5battRCaNJK / 23GGpCsTQatJN8PZBhJBb2Vlsvw5lFrSdMT1R7vaz + 2EeNR / FitFXwIDAQAB'

how to import it and use it to encrypt a string?

如何导入它并使用它来加密字符串?

I tried the following code but RSA.construct() raises exception (TypeError: must be long, not str).

我尝试了以下代码,但 RSA.construct() 引发异常(TypeError: must be long, not str)。

from Crypto.PublicKey import RSA
from Crypto.Util import asn1
from base64 import b64decode

keyDER = b64decode(pubkey)
seq = asn1.DerSequence()
seq.decode(keyDER)
keyPub = RSA.construct((seq[0], seq[1]))
print keyPub.encrypt('mysecret', 32)

Thanks.

谢谢。

回答by Ber

From the documentation

文档

importKey(externKey, passphrase=None)
Import an RSA key (public or private half), encoded in standard form.

回答by EwyynTomato

By using:

通过使用:

RSA.importKey(externKey)

with parameter externKey look like the following:

带有参数 externKey 如下所示:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAybVqRvfYvWbLsB98BqkD
lWd0/5y6SyhHt6/r6M0l7JXBweqMvxVt7XmI2yqPL56YxzcgQ8ycDkoqHJ+XozgP
iRnLNpYRlCzsiaOElbmQcnrI8iOb9Ahm6j0cbBB1S8VNvD+u9RQJt53zPxPj8/Dq
f1oNGFXOM8udNYWZaRCukLs/TumsAn0a+BF4639WtFiUvTWdVhlyvCQTs49ytRkH
rXH30RkB528RIvTGeW8xBTV4NaiTIzAEKCVSPagLr4Hzbb9b5+bODic/zkLGQazy
/NKOFgiB7kD2+WEMcuhTr5noeXau0PDAhgmrBhzzWOjUwwaO+ACvJLkPXZfjhy7P
+wIDAQAB
-----END PUBLIC KEY-----

You shouldn't b64decode the externKey and the string should start with "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----".

你不应该对 externKey 进行 b64decode 并且字符串应该以“-----BEGIN PUBLIC KEY-----”和“-----END PUBLIC KEY-----”开头。

回答by Chet

I too had trouble with this. I got it working like this:

我也遇到了这个问题。我让它像这样工作:

key = RSA.generate(2048)

binPrivKey = key.exportKey('DER')
binPubKey =  key.publickey().exportKey('DER')

privKeyObj = RSA.importKey(binPrivKey)
pubKeyObj =  RSA.importKey(binPubKey)

msg = "attack at dawn"
emsg = pubKeyObj.encrypt(msg, 'x')[0]
dmsg = privKeyObj.decrypt(emsg)

assert(msg == dmsg)

If you're writing to files, you may find it easier to deal with hex strings instead of binary strings. I'm using these helper functions a lot

如果您正在写入文件,您可能会发现处理十六进制字符串而不是二进制字符串更容易。我经常使用这些辅助函数

def bin2hex(binStr):
    return binascii.hexlify(binStr)

def hex2bin(hexStr):
    return binascii.unhexlify(hexStr)

回答by user9527

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
from base64 import b64decode,b64encode

pubkey = 'MIGfMA0GCSqGSIb3DQEBA3UAA4GNADCBiQKBgQC35eMaYoJXEoJt5HxarHkzDBEMU3qIWE0HSQ77CwP/8UbX07W2XKwngUyY4k6Hl2M/n9TOZMZsiBzer/fqV+QNPN1m9M94eUm2gQgwkoRj5battRCaNJK/23GGpCsTQatJN8PZBhJBb2Vlsvw5lFrSdMT1R7vaz+2EeNR/FitFXwIDAQAB'
msg = "test"
keyDER = b64decode(pubkey)
keyPub = RSA.importKey(keyDER)
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode())
emsg = b64encode(cipher_text)
print emsg

回答by u7972580

thanks @user9527 vote up for you

谢谢@user9527 为你投票

that solved my problem

这解决了我的问题

my env: win10x64 python3.6.4 pycrypto2.6.1

我的环境:win10x64 python3.6.4 pycrypto2.6.1

here's my code, encrypt end decrypt, the key was from someone's blog.(if U occured with "ValueError: RSA key format is not supported", check the key format, it should be warpped with some thing like "-----BEGIN XXXX KEY-----")

这是我的代码,加密结束解密,密钥来自某人的博客。(如果您出现“ValueError:不支持RSA密钥格式”,请检查密钥格式,它应该被扭曲为“----- BEGIN XXXX 键-----")

pubkey = """-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----"""

prvkey = """-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----"""

from Crypto.PublicKey import RSA

from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5

msg = "test"
print("raw msg->", msg)
keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
#print(cipher.encrypt.__doc__)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)


keyPriv = RSA.importKey(prvkey) # import the private key
cipher = Cipher_PKCS1_v1_5.new(keyPriv)
#print(cipher.decrypt.__doc__)
decrypt_text = cipher.decrypt(cipher_text, None).decode()
print("decrypted msg->", decrypt_text)
assert msg == decrypt_text # check that
print("test passed")

the output:

输出:

raw msg-> test
cipher text-> b'\xb0]\x1f@B\x8b\xb5\xbf\x891:\t4D\x80$\xc0y\xaa\xb4\x86t/|\xeaM%\xf06\x14,\x9e?\x86R\x83\xd72\xe5\xfdsr:\x99\xe7v\xd9]&\xbc\x85\xd3\x16\x80\x19q\xe7\xb1\x89\xff/\x12\xe5\xb3\x9cu\x1f\x04x\xa5\xdfl\xcd\xae_\xba\x1b\x97\x9fa\xcf9O\xbfB\xf6\xd1N\xf5|<\xbf^\x84R\xecSo\x9a*\xf7\x8d\x8e\xbe0Q\xcd\x14\x13\xf98x\xe7\xd8x\x19\xaf\x98\xefu\xa8\xb1\xd3\xfa\xf2N\xca\xb5'
decrypted msg-> test
test passed

回答by Ari

The method I ended up using based on a few answers here:

我最终使用的方法基于这里的一些答案:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5

def encrypt_data(data):
    with open("/path/to/public.pem", "rb") as k:
        key = RSA.importKey(k.read())

    cipher = Cipher_PKCS1_v1_5.new(key)
    return cipher.encrypt(data.encode())


def decrypt_data(data):
    with open("path/to/private.pem", "rb") as k:
        key = RSA.importKey(k.read())

    decipher = Cipher_PKCS1_v1_5.new(key)
    return decipher.decrypt(data, None).decode()


message = "hello world!"
encrypted = encrypt_data(message)
decrypted = decrypt_data(message)