为什么在此代码中出现异常: 我得到输出:
[*]创建密钥时出错
[*]创建密钥时出错
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。
答案 0 :(得分:1)
您应该了解并知道该问题,但我认为您的问题是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)
还要检查您的语法,为什么该代码没有得到很好的尝试