问题描述
通过使用以下命令,我已经能够使用DES3创建受密码保护的RSA密钥(嗯...我认为,因为我对这个加密世界非常新) >
openssl genrsa -out/tmp/myKey.pem-passout pass:f00bar-des3 2048
如果可能的话,我想在Python脚本中使用PyCrypto。我看到了,似乎以阻止使用PyCrypto来做到这一点。还是这样吗?
当然可以随时调用 os.execute
,并执行上述命令,但我认为欺骗: - )。我非常喜欢学习PyCrypto。
提前谢谢。
从PyCrypto 2.5开始,您可以导出RSA私钥并使其受密码保护。三重DES密钥从密码短语内部导出,用于执行实际加密。
例如:
<$来自Crypto import RSA
从$ Cr $ import
random_generator = Random.new()。read
key = RSA.generate(1024 ,random_generator)
exportsKey = key.exportKey('PEM','我的秘密',pkcs = 1)
变量 exportedKey
包含密钥的ASCII版本(PEM),根据PKCS#1(加密标准)进行编码,另一个选项是 pkcs = 8
为 - 猜猜 - PKCS#8)。由于结果是标准的,因此可以使用其他几个程序,包括openssl。
当然你也可以通过PyCrypto重新导入到python!
exportKey
方法记录为。
I have been able to create a RSA key protected by password with DES3 (well... I think because I'm very new to this encryption world) by using the command:
openssl genrsa -out "/tmp/myKey.pem" -passout pass:"f00bar" -des3 2048
Now, I would like to do that inside a Python script, using PyCrypto, if possible. I have seen this message, which seems to discourage the use of PyCrypto to do that. Is it still like that?
Of course I can always call os.execute
, and execute the above command, but I'd consider that "cheating" :-). I'm pretty much doing this to learn PyCrypto.
Thank you in advance.
Starting from PyCrypto 2.5 you can export an RSA private key and have it protected under a passphrase. A Triple DES key is internally derived from the passphrase and used to perform the actual encryption.
For instance:
from Crypto import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
exportedKey = key.exportKey('PEM', 'my secret', pkcs=1)
The variable exportedKey
contains an ASCII version (PEM) of the key, encoded according to PKCS#1 (a cryptographic standard. Another option is pkcs=8
for - guess what - PKCS#8). Since the result is standard, you can use it with several other programs, including openssl.And of course, you can also re-import it back into python via PyCrypto!
The exportKey
method is documented here.
这篇关于PyCrypto:生成使用DES3密码保护的RSA密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!