' openssl rsa -in -in' python的命令

发布于 2025-02-06 03:05:47 字数 624 浏览 1 评论 0 原文

我正在尝试从使用Python subprocess.run()生成的私钥生成公共密钥,其中我将私钥存储在变量中,而不是在文件中。我想将相同的缓冲区变量与OpenSSL RSA命令的输入一起使用,作为输入,它可以用来生成公共密钥。我不确定如何在python代码中通过此缓冲区 -

#generating private key and keeping it in a variable
filedata = subprocess.run(['openssl', 'genrsa', '4096'], check=True, stdout=subprocess.PIPE, universal_newlines=True).stdout

现在使用openssl rsa命令中的filedata,如何在os.system()中的python命令中传递此缓冲区,或者

os.system("openssl rsa -in private.key -pubout > public.key")

可以通过subprocess.run()传递private.key数据从将数据保存到OpenSSL RSA的变量?在这里,我避免将私钥内容存储在文件中。这就是为什么我使用归档变量的原因。

感谢您的答复。

I am trying to generate public key from private key generated using python subprocess.run() where I store the private key in a variable, not in a file. I want to use the same buffer variable as input to openssl rsa command as input which it can use to generate the public key. I am not sure how to pass this buffer in python code-

#generating private key and keeping it in a variable
filedata = subprocess.run(['openssl', 'genrsa', '4096'], check=True, stdout=subprocess.PIPE, universal_newlines=True).stdout

Now using that filedata in openssl rsa command, how to pass this in python command in os.system() or subprocess.run()

os.system("openssl rsa -in private.key -pubout > public.key")

Is it possible to pass private.key data from the variable which holds the data to openssl rsa? Here I am avoiding to store the private key contents in a file. That is why I am using the filedata variable.

thanks for your reply.

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

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

发布评论

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

评论(1

绝不服输 2025-02-13 03:05:47

如果您不提供 -in 选项,则 stdin openssl rsa 。因此,使用 subprocess.run ,您可以使用与您使用的命令相似的命令来提供 filedata 作为输入。

首先,让我们修改您的私钥生成,以便从 subprocess.run 而不是字符串获得字节:

filedata = subprocess.run(
    ["openssl", "genrsa", "4096"],
    check=True,
    stdout=subprocess.PIPE,
).stdout

然后,我们可以将 filedata 作为输入作为 openssl rsa-- Pubout ...
在没有 -in in 选项的情况下,将从 stdin 读取
默认值:

subprocess.run(
    ["openssl", "rsa", "-pubout", "-out", "public.key"], input=filedata, check=True
)

现在您在文件 public.key 中拥有公共密钥。如果你愿意
将公共密钥在变量中而不是文件中,您可以删除
-out public.key 选项,在这种情况下,命令几乎是
与第一个相同:

pubkey = subprocess.run(
    ["openssl", "rsa", "-pubout"],
    input=filedata,
    check=True,
    stdout=subprocess.PIPE,
).stdout

If you don't provide a -in option, then openssl rsa by default reads from stdin. So using subprocess.run, you can provide filedata as input using a command very similar to the one you're using to generate the private key.

First, let's modify your private key generation so that we get bytes from subprocess.run instead of a string:

filedata = subprocess.run(
    ["openssl", "genrsa", "4096"],
    check=True,
    stdout=subprocess.PIPE,
).stdout

We can then pass filedata as input to openssl rsa -pubout ...,
which in the absence of a -in option will read from stdin by
default:

subprocess.run(
    ["openssl", "rsa", "-pubout", "-out", "public.key"], input=filedata, check=True
)

Now you have the public key in file public.key. If you would rather
have the public key in a variable instead of in a file, you can remove
the -out public.key option, in which case the command is almost
identical to the first one:

pubkey = subprocess.run(
    ["openssl", "rsa", "-pubout"],
    input=filedata,
    check=True,
    stdout=subprocess.PIPE,
).stdout
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文