Pyopenssl RSA私钥与AES 256加密

发布于 2025-02-13 15:47:07 字数 316 浏览 0 评论 0原文

在Pyopenssl中,我还没有找到一种使用AES 256加密RSA私钥的方法,一直在寻找此处,但似乎无法找到一种方法。

在我使用OpenSSL获取密钥和CA/CL证书之前,但是现在我选择在需要以某些方式处理PFX文件的应用程序中进行应用程序。

在openssl中,我曾经做过以下操作:

openssl pkcs12 -in file.pfx -nocerts -nocerts -out key.key

在那之后:

openssl rsa -aes256 -key.key.key-key -out negpted.key

In pyOpenSSL i haven't been able to find a way to encrypt a RSA private key with AES 256 just yet, been looking all over the place for this but cant seem to find a way.

Before i used OpenSSL to get the key and ca/cl certificates but now im opting to make an application where i need to handle the pfx-file in certain ways.

In OpenSSL i used to do the following:

openssl pkcs12 -in file.pfx -nocerts -out key.key

after that i did:

openssl rsa -aes256 -key.key -out encrypted.key

is there anything similar in pyOpenSSL using crypto?

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

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

发布评论

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

评论(1

趁年轻赶紧闹 2025-02-20 15:47:07

我相信我解决了这个问题。但是对于任何想知道的人来说,这就是我所做的:

import os
import shutil
from Crypto.PublicKey import RSA


def encrypt(old_key, new_key, passphrase):
        key = RSA.importKey(open(old_key, 'rb').read())

        with open(new_key, 'wb') as f:
                pem_key = key.export_key(format='PEM', passphrase=passphrase, pkcs=8, protection='PBKDF2WithHMAC-SHA1AndAES256-CBC')

                f.write(pem_key)
                f.close()

        if os.path.exists(old_key):
                os.remove(old_key)


encryptAES('path_to_old_key', 'path_to_new:key.key', 'supersecretpassword')

仍然剩下的一个问题是,无论如何是否有与OpenSSL相似的Python中完成的加密信息?

如果您运行openssl rsa -aes256 -in old.key -out new.key

键将在开始时返回属性:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC
Key here...
-----END RSA PRIVATE KEY-----

但是,当我在python中导出私钥时,我会得到

-----BEGIN ENCRYPTED PRIVATE KEY-----
Key here...
-----END ENCRYPTED PRIVATE KEY-----

:无论如何,都用pycryptodome显示这些属性?

I believe I solved this. But for anyone wondering, this is what I did:

import os
import shutil
from Crypto.PublicKey import RSA


def encrypt(old_key, new_key, passphrase):
        key = RSA.importKey(open(old_key, 'rb').read())

        with open(new_key, 'wb') as f:
                pem_key = key.export_key(format='PEM', passphrase=passphrase, pkcs=8, protection='PBKDF2WithHMAC-SHA1AndAES256-CBC')

                f.write(pem_key)
                f.close()

        if os.path.exists(old_key):
                os.remove(old_key)


encryptAES('path_to_old_key', 'path_to_new:key.key', 'supersecretpassword')

One question still remaining is if there's anyway to output the encryption info done in python similar to OpenSSL?

If you run openssl rsa -aes256 -in old.key -out new.key

The key will return attributes in the beginning like such:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC
Key here...
-----END RSA PRIVATE KEY-----

However when I export the private key in Python I just get:

-----BEGIN ENCRYPTED PRIVATE KEY-----
Key here...
-----END ENCRYPTED PRIVATE KEY-----

Is there anyway to display these attributes with pycryptodome?

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