繁体   English   中英

生成RSA并写入文件

[英]Generating RSA and writing to file

为什么在此代码中出现异常:得到输出:

[*]创建密钥时出错

[*]创建密钥时出错

import os, hashlib
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA

raw_key = RSA.generate(2048)
private_key = raw_key.exportKey('PEM')
try:
with open('master_private.pem', 'w+') as keyfile:
    keyfile.write(private_key)
    keyfile.close()
print ("[*] Successfully created your MASTER RSA private key")
except:
print ("[*] Error creating your key")

make_public = raw_key.publickey()
public_key = make_public.exportKey('PEM')
try:
with open("master_public.pem", "w+") as keyfile:
    keyfile.write(public_key)
    keyfile.close()
print ("[*] Successfully created your MASTER RSA public key")
except:
print ("[*] Error creating your key")

文件创建成功,但未填充任何内容。 我刚刚开始使用Python。

您应该了解并显示出知道的问题,但是我认为您的问题是write方法,private_key其字节,但是您必须通过str来编写可以尝试的方法:

   keyfile.write(private_key.decode())

另一个问题可能是您的许可权限,可能没有创建文件的权限,尝试捕获说明并打印以了解会发生什么

try:
    with open('master_private.pem', 'w+') as keyfile:
    keyfile.write(private_key)
    keyfile.close()
    print ("[*] Successfully created your MASTER RSA private key")
except Exception as e:
    print ("[*] Error creating your key", e)

还要检查您的语法,为什么该代码没有得到很好的尝试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM