如何使用Python OpenSSL公共密钥对字符串进行加密?

发布于 2025-02-01 04:35:17 字数 379 浏览 2 评论 0 原文

我当前生成OpenSSL公私密钥对的代码:

import OpenSSL

key = OpenSSL.crypto.PKey()
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
public_key = OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, key)
private_key = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)
print(public_key, private_key, sep="\n")

My current code to generate an OpenSSL public-private key pair:

import OpenSSL

key = OpenSSL.crypto.PKey()
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
public_key = OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, key)
private_key = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)
print(public_key, private_key, sep="\n")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-08 04:35:17

该文档建议您调用要获取一个加密密钥,然后使用Pyca/加密进行加密。

https://www.pypyopenssl.org.org/en/en/stable/stable/pable/api/api/api/crypto。 html

鉴于您的加密密钥:

result = crypto_key.encrypt(b"Hello there")

我应该补充,RSA是主要供签名和验证以及密钥交换的主要。对于一般加密来说太慢了。

The documentation recommends that you call key.to_cryptography_key() to get a cryptographic key, and then use pyca/cryptography to perform the encryption.

https://www.pyopenssl.org/en/stable/api/crypto.html

Given your crypto key:

result = crypto_key.encrypt(b"Hello there")

I should add, RSA is primary intended for signatures and verification, and key exchange. It's way too slow for general encryption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文