擅长:python、mysql、java
<p>我知道这是一个老问题-但我刚找到它,我想我会补充一个答案。</p>
<p>使用Python 3.x实现这一点的最简单方法是使用<a href="https://pycryptodome.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">PyCryptodome</a>。</p>
<p>在Python中(对于2048位密钥):</p>
<pre><code>from Cryptodome.PublicKey import RSA
key = RSA.generate(2048)
pv_key_string = key.exportKey()
with open ("private.pem", "w") as prv_file:
print("{}".format(pv_key_string.decode()), file=prv_file)
pb_key_string = key.publickey().exportKey()
with open ("public.pem", "w") as pub_file:
print("{}".format(pb_key_string.decode()), file=pub_file)
</code></pre>
<p>如果要在(Linux)命令行上检查私钥,请使用:</p>
<pre><code>$ openssl rsa -check -inform pem -noout -in private.pem
RSA key ok
...
</code></pre>