我正在尝试从使用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.
发布评论
评论(1)
如果您不提供 openssl rsa 。因此,使用
-in
选项,则 stdinsubprocess.run
,您可以使用与您使用的命令相似的命令来提供filedata
作为输入。首先,让我们修改您的私钥生成,以便从
subprocess.run
而不是字符串获得字节:然后,我们可以将
filedata
作为输入作为openssl rsa-- Pubout ...
,在没有
-in in
选项的情况下,将从 stdin 读取默认值:
现在您在文件
public.key
中拥有公共密钥。如果你愿意将公共密钥在变量中而不是文件中,您可以删除
-out public.key
选项,在这种情况下,命令几乎是与第一个相同:
If you don't provide a
-in
option, thenopenssl rsa
by default reads from stdin. So usingsubprocess.run
, you can providefiledata
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:We can then pass
filedata
as input toopenssl rsa -pubout ...
,which in the absence of a
-in
option will read from stdin bydefault:
Now you have the public key in file
public.key
. If you would ratherhave 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 almostidentical to the first one: