gpt4 book ai didi

python - 不支持在python中使用pem文件加密json给出RSA key 格式

转载 作者:行者123 更新时间:2023-12-04 11:23:43 26 4
gpt4 key购买 nike

我正在研究一个加密模块,其中请求采用加密的 JSON 形式的参数。这种加密是由服务器提供的公钥完成的,如下所示,

def encrypt_bank_request(request_payload):
key = ""
url = "http://localhost/payment/gateways/mybank.pem"
file = urllib.request.urlopen(url)
decoded_file = file.read().decode("utf-8")
for line in decoded_file:
key += line

keyDER = base64.b64decode(key)
keyPub = RSA.importKey(keyDER)
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(request_payload.encode())
emsg = base64.b64encode(cipher_text)
print(emsg);
但是当我执行代码时,我在 keyPub = RSA.importKey(keyDER) 得到关注,

ValueError at /

RSA key format is not supported


相当于我想要实现的 PHP 如下,
$pemKey = file_get_contents("http://localhost/payment/gateways/mybank.pem");
openssl_public_encrypt($request_payload, $encrypted, $pemKey);
openssl_public_encryptdocumentation建议使用公钥加密数据并将结果存储到 $encrypted .
我在这里做错了什么?是不是因为该函数需要 RSA key 对象,而我提供的是字符串,在这种情况下如何将字符串转换为 RSA key 对象。
我引用了以下内容,
https://stackoverflow.com/a/46356449/11782743
谢谢你的建议。
附言我的 key 如下,
MIIGPjCCBSagAwIBAgIRAJig5hCghJQ8AAAAAFDbeaEwDQYJKoZIhvcNAQELBQAwgboxCzAJBgNV
BAYTAlVTMRYwFAYDVQQKEw1FbnRydXN0LCBJbmMuMSgwJgYDVQQLEx9TZWUgd3d3LmVudHJ1c3Qu
bmV0L2xlZ2FsLXRlcm1zMTkwNwYDVQQLEzAoYykgMjAxMiBFbnRydXN0LCBJbmMuIC0gZm9yIGF1
dGhvcml6ZWQgdXNlIG9ubHkxLjAsBgNVBAMTJUVudHJ1c3QgQ2VydGlmaWNhdGlvbiBBdXRob3Jp
dHkgLSBMMUswHhcNMTcwMjE1MDYyMzM5WhcNMTgwMjE0MDY1MzM3WjBxMQswCQYDVQQGEwJJTjEO
MAwGA1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
ZW50cnVzdC5uZXQvcnBhMAgGBmeBDAECAjBoBggrBgEFBQcBAQRcMFowIwYIKwYBBQUHMAGGF2h0
dHA6Ly9vY3NwLmVudHJ1c3QubmV0MDMGCCsGAQUFBzAChidodHRwOi8vYWlhLmVudHJ1c3QubmV0
L2wxay1jaGFpbjI1Ni5jZXIwIwYDVR0RBBwwGoIYZWF6eXBheWFwaS5pY2ljaWJhbmsuY29tMB8G
A1UdIwQYMBaAFIKicHTdvFM/z3vU981/p2DGCky/MB0GA1UdDgQWBBSG2RKU6li1ezRacfLkf5Tr
kECMPjAJBgNVHRMEAjAAMA0GCSqGSIb3DQEBCwUAA4IBAQBvVGUQGLBhs9GNpNjLTVdc3WY5pYQE
5fP/otaX7GBx4bRrRZlPPh/vzIg79Ry9vs/GdvZIiyVczKZeB2ih9PKJySWEPXPgnR+aroHnQMVK
hOhBoKcohtpUmjnQLgL400h6NkQ9GS0yDebLlbJxicGIAhq+OSJhUXeYKLIk38ngPCYwL+PjHPn9
1ds0ehCuOFMuKKaY4e+hsKzc8KZPyTM7hbtw86kbheOizTGQ8M9s8ZTRnTYblSPk5w5A3fqaikG7
bAYKNWcdBGgdOCnHHCDPSP0ghtf4klR1tT99PSW1HHZ/VL8tmvw+/YNXzIdNxB+MPm3OM/A8Dz6i
khpZKNeP

最佳答案

您需要使用 PyCryptodome ,而不是 PyCrypto,并且 key 必须是具有正确 -----BEGIN...-----END... 行的 base-64,或者您必须自己对 base64 编码的 blob 进行 base64 解码并将结果字节提供给 RSA.import_key()

from Cryptodome.PublicKey import RSA

cert = '''MIIGPjCCBSagAwIBAgIRAJig5hCghJQ8AAAAAFDbeaEwDQYJKoZIhvcNAQELBQAwgboxCzAJBgNV
BAYTAlVTMRYwFAYDVQQKEw1FbnRydXN0LCBJbmMuMSgwJgYDVQQLEx9TZWUgd3d3LmVudHJ1c3Qu
bmV0L2xlZ2FsLXRlcm1zMTkwNwYDVQQLEzAoYykgMjAxMiBFbnRydXN0LCBJbmMuIC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gMBMGA1UdJQQMMAoG
CCsGAQUFBwMCMDMGA1UdHwQsMCowKKAmoCSGImh0dHA6Ly9jcmwuZW50cnVzdC5uZXQvbGV2ZWwx
ay5jcmwwSwYDVR0gBEQwQjA2BgpghkgBhvpsCgEFMCgwJgYIKwYBBQUHAgEWGmh0dHA6Ly93d3cu
ZW50cnVzdC5uZXQvcnBhMAgGBmeBDAECAjBoBggrBgEFBQcBAQRcMFowIwYIKwYBBQUHMAGGF2h0
dHA6Ly9vY3NwLmVudHJ1c3QubmV0MDMGCCsGAQUFBzAChidodHRwOi8vYWlhLmVudHJ1c3QubmV0
L2wxay1jaGFpbjI1Ni5jZXIwIwYDVR0RBBwwGoIYZWF6eXBheWFwaS5pY2ljaWJhbmsuY29tMB8G
A1UdIwQYMBaAFIKicHTdvFM/z3vU981/p2DGCky/MB0GA1UdDgQWBBSG2RKU6li1ezRacfLkf5Tr
kECMPjAJBgNVHRMEAjAAMA0GCSqGSIb3DQEBCwUAA4IBAQBvVGUQGLBhs9GNpNjLTVdc3WY5pYQE
5fP/otaX7GBx4bRrRZlPPh/vzIg79Ry9vs/GdvZIiyVczKZeB2ih9PKJySWEPXPgnR+aroHnQMVK
hOhBoKcohtpUmjnQLgL400h6NkQ9GS0yDebLlbJxicGIAhq+OSJhUXeYKLIk38ngPCYwL+PjHPn9
1ds0ehCuOFMuKKaY4e+hsKzc8KZPyTM7hbtw86kbheOizTGQ8M9s8ZTRnTYblSPk5w5A3fqaikG7
bAYKNWcdBGgdOCnHHCDPSP0ghtf4klR1tT99PSW1HHZ/VL8tmvw+/YNXzIdNxB+MPm3OM/A8Dz6i
khpZKNeP'''

cert = '-----BEGIN CERTIFICATE-----\n' + cert + '\n-----END CERTIFICATE-----'


r = RSA.importKey(cert)
print(r.n)

打印出模数
835530417319735322904421462459451063654010910036490131542528939590706305612565738190465258970018362442504314027037982757378964476628610126628643741295376455983157717853926501344173174543509081666215334337886432121330318119586463591986644236407155160633403615394726359135900210267964618093240043978235544727335109686553571844741498488578037894004729516832328328853475766210938149668579224549929585498616511012055116486617256405348752281221841113593203898211327673790225447988279497436707544397419647915422702720143667984437532004226144606948907602116888194895153345334011560415740885466004583157439110458828130795408434017885187061379936745660293815100292079970030432352403962499538301054633885062147509036462581226410523337358445683232465510709519096907784307249725979290562163532705329623550196710312110800265447584580251840590731044582860060553698109052225806921430613417139832332963970234067131600542381764969722189768692969004592637189607748143035304212536331626692025074578149324603263114533612082445070491074737755256358022645183110670751682994979892626789172684974128522083229506601674218910572247739238520579221342029941195041299137018203411248036919473974612176366220589057599358170063558334757947393372489019738067582542051

关于python - 不支持在python中使用pem文件加密json给出RSA key 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62245039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com