我正在使用PyCrypto 2.3,我想将生成的密钥保存到文件中,以便将它们分发到客户端和服务器。我似乎无法找到正确打印密钥的方法,也无法在互联网上找到示例。
def resetKeys(self):
keys = RSA.generate(1024)
privHandle = open(self.privateKeyFile, 'wb')
privHandle.write(keys.privatekey())
privHandle.close()
pubHandle = open(self.publicKeyFile, 'wb')
pubHandle.write(keys.publickey())
pubHandle.close()
此代码不起作用的原因有很多:首先,keys.publickey()
似乎不可打印,它会返回:
<_RSAobj @0x10f810d0 n(1024),e>
和第二,keys
没有名为privatekey
的函数。
以前有人这样做过吗?
答案 0 :(得分:31)
keys.exportKey()
表示私钥,keys.publickey().exportKey()
表示公钥。您可以使用format
参数更改输出格式,请参阅docs at this site。
答案 1 :(得分:3)
以下代码将创建RSA密钥对,并将它们存储在PEM文件中并打印出来。
原始信用:此帖子中的wRAR&amp;来自[pyOpenSSL creating a pem file
的AJ poultierfrom Crypto.PublicKey import RSA
private_key = RSA.generate(1024)
public_key = private_key.publickey()
print(private_key.exportKey(format='PEM'))
print(public_key.exportKey(format='PEM'))
with open ("private.pem", "w") as prv_file:
print("{}".format(private_key.exportKey()), file=prv_file)
with open ("public.pem", "w") as pub_file:
print("{}".format(public_key.exportKey()), file=pub_file)
答案 2 :(得分:0)
这是使用 PKCS -> DER SEQUENCE(二进制或 PEM 编码)生成长度为 2048 的密钥的示例
from Crypto.PublicKey import RSA
private_key_start_comment = '-----BEGIN RSA PRIVATE KEY-----'
private_key_end_comment = '-----END RSA PRIVATE KEY-----'
public_key_start_comment = '-----BEGIN PUBLIC KEY-----'
public_key_end_comment = '-----END PUBLIC KEY-----'
keypair = RSA.generate(2048)
pubkey = keypair.publickey()
public_key_pem = pubkey.exportKey(format='PEM', passphrase=None, pkcs=1)
public_key_final = str(public_key_pem.decode('ascii')).replace('\n','').replace(public_key_start_comment,'').replace(public_key_end_comment,'').replace(' ','')
print('\n public_key_final - ' + public_key_final)
private_key_pem = keypair.exportKey(format='PEM', passphrase=None, pkcs=1)
private_key_final = str(private_key_pem.decode('ascii')).replace('\n','').replace(private_key_start_comment,'').replace(private_key_end_comment,'').replace(' ','')
print('\n private_key_final - ' + private_key_final)
Sample Output:
public_key_final - MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvUC4fYGPHPTraHoozf8xkSwHL4pPqcleSTtiOXvx8X4eJ7D8UAFSi5EkgGfNhzM/EWTUfPLuAH948eJorB25giVnJRzEShvsBVFVF0uTS9avwQWD1EWIoc60cH4l6GuqoAckLtqpUOvHglwBJvjeR4WenxQHV/U7WgJfou04RZQ3sosrg1hfNoWJo79LUzjzPHe+WKqXri0rtLj/JAKofQzs1mWkRKvZkGvWVk/AjCurKs/bjMuCmBd7fddL3XvOAu7bw+tETFyT54nkIOQq2Sw4I8fvcNeA6uNJL6EZMUgSj2GYXAJLOc+bwon6fH95LwaXLliMgF4FXjMlo5sbfwIDAQAB
private_key_final - MIIEogIBAAKCAQEAvUC4fYGPHPTraHoozf8xkSwHL4pPqcleSTtiOXvx8X4eJ7D8UAFSi5EkgGfNhzM/EWTUfPLuAH948eJorB25giVnJRzEShvsBVFVF0uTS9avwQWD1EWIoc60cH4l6GuqoAckLtqpUOvHglwBJvjeR4WenxQHV/U7WgJfou04RZQ3sosrg1hfNoWJo79LUzjzPHe+WKqXri0rtLj/JAKofQzs1mWkRKvZkGvWVk/AjCurKs/bjMuCmBd7fddL3XvOAu7bw+tETFyT54nkIOQq2Sw4I8fvcNeA6uNJL6EZMUgSj2GYXAJLOc+bwon6fH95LwaXLliMgF4FXjMlo5sbfwIDAQABAoIBAA1fIcnoWgrDOVDP9MsdQUzPDhDrVlaaPjmxYu7J1Z3o+ct3I1Spy0HGNwyFiQ2I7udVNvg/F9bcKtj4VMn4ba+G4gNKxYmw8Fut0/PJ6LXejNt3/skEQrOgDbq3BwmlddBJwOI4m+b2Scnpo0E0K0xlYgA+Y4hw8MkaytQRq7yqrW31uq7ueN2eJH+akpBiG7MWfEqP3UonWhSfl2yGIc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