gpt4 book ai didi

python - 密码学 - 生成新的 RSA 私钥

转载 作者:行者123 更新时间:2023-11-28 22:13:01 27 4
gpt4 key购买 nike

我需要一个函数来生成一个新的 RSA 私钥,然后可以将其作为字符串存储在 Django 模型字段中。我正在使用 cryptography==2.1.4 .

我认为我已经通过以下方式实现了这一目标:

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend

KEY_SIZE = 2048
PUBLIC_EXP = 65537
private_key = rsa.generate_private_key(
public_exponent=PUBLIC_EXP,
key_size=KEY_SIZE,
backend=default_backend()
)
private_key_str = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
).decode()

但显然我在这里遗漏了一些东西或一些步骤(尽管我应该知道为什么......)因为 private_key_str 始终具有相同的值。

这里有什么建议吗?

最佳答案

我用 cryptography==2.1.4 尝试你的代码,我发现 private_key_str 不一样。我想你可能没有仔细比较这两个字符串。

它们都使用 PEM 编码的 PKCS#1 结构来对 key 进行编码,因此它们具有相似的结构。

private_key1

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAspdHsnVqiY0RFYVAHcqa9AQvZC4u7gGv36wWCEUIlemJx2L7
1UvAbD5b/n/RPp4nw2xTIpSFguC+d4t5IaomQR7WR0aCcWda9SveNk4KyJmnHUox
Z6BBz4vBhdLWLLwZwxGsmuml60Wv5JMVEbf0lDgZqjIBMH0W5hFxa6f3AvI2L2Xf
LzwhfcyDMBIENx47aDFyWwfJvkIotFcLS9gYvo5KQXCdoHMbkoygF2cF+JAfSPEb
HaRw0qCOIDb0pC/L8HUxyiSeGH9eu93KyweRTqoXdkWdmKLYlJHUsdv24xrUxeA3
rARho2flkL/kz6AKO4NJJy+pvDCCtk8PW+HnTwIDAQABAoIBAD8587Yof5lHCf9W
d4aUjhtJyAVtnEvC6SME55/x4MJCNfcTAhlXgj5QLX2Cfu/uSiGTn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zVAXHMVY9rRLa7Iqdot+OnzF86T7RW/F8T5md3y6lJX2Y1HdHxLDYPm1xXBo
gg7Du/Eq/+L24YGJzg+CQs00Z3l5mJxt9OSsjuXqFJQqf9PeJUeNkhdY77xuzBfH
tS2dChmRfwLRqaatYq4RZ4s4tByib/8vjo1RMtkCgYAA5Qth0GveYBwzNF1RxpMb
ZWaoDnNqNMUF4+3b2mlb53mjVMDWZ3tiR/6ayYHYSbGad2Et6nlaCPm/eCIi5E6r
6bkGKuOlcFJCre+VNMtTQ3Y1OYVBXERb1ZPNpjZkn+MEWyBuKE2PxirrohqFASpz
D/TjL30QKBb6dksPEy9MyQKBgQCN7r+pOQq+K6Vl9Se2AvDtDjIOSQIyPIX9hdS/
++Z2dsAYa/3WaZPuVsdVJDJcWdlv8qhmp62gmwMDsqRDsB07kUY1Q825yjTWJpQs
B6SXoqN1s7JrSajV+eJJP66CmkYPHYJBgSoMj4mPsgvwj+XfxBJRuAwMthth2Swa
9wyzUQKBgCp9eBK/0mgEjRr1JacjDVZEcsIcj/erGA2FwoGRu1fXPevi1ggyJIKU
DA+aRZehHqUVOafNQsMJafMqFTTJxYtgkzMRqxfDz6HpNC1hWyvE5/STswEl5Izu
l2QSz4TU1Yk2wnrtDlIVkb6gM/JkQiOkSG5PSx9EAACZ1Yoz+NqS
-----END RSA PRIVATE KEY-----

关于python - 密码学 - 生成新的 RSA 私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54343899/

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