【发布时间】:2019-10-01 15:00:46
【问题描述】:
我正在为学校学习如何使用公钥和私钥编码来加密和解密文件。
我使用此代码对消息进行编码。 (产生公钥≠不是私钥错误)
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
def signing():
#open file = message als binary
message = open('C:/Users/Gebruiker/Desktop/message.txt', "rb").read()
#open public key -> key
key = RSA.import_key(open('C:/Users/Gebruiker/Desktop/public.pem').read())
#message becomes a hash
h = SHA256.new(message)
#f = open file as write binary
f = open('C:/Users/Gebruiker/Desktop/message.signature', 'wb')
# sign hash message with private key
signature = pkcs1_15.new(key).sign(h)
#write signed hash to file
f.write(signature)
f.close()
但现在我正在尝试解码此消息,我发现所有这些人都以不同的方式进行解码,并使用不同类型的编码和加密。而且我找不到明确的答案。
我现在拥有的是这个
首先我必须阅读消息
def decrypt():
f = open('C:/Users/Gebruiker/Desktop/message.signature', 'rb').read()
然后我打开我的私钥
key = RSA.import_key(open('C:/Users/Gebruiker/Desktop/private.pem').read())
因为我用二进制写和读,我必须把它转回散列
h = SHA256.new(f)
然后我必须使用我的私钥解密哈希。???
相反,我看到很多人使用这样的东西。
h = pkcs1_15.new(key).sign(h) # sign hash message with private key
我不明白。你应该解码它吧?不要再签了。这部分对我来说毫无意义。
现在我有 2 个问题。
- 我收到一个编码错误,提示我的公钥不是私钥。这有点像公钥加密的重点。所以只有私钥可以解密?为什么我会收到错误消息?
- 我不知道如何继续解密我的消息
谁能帮我解决这个问题?
非常感谢!
【问题讨论】:
标签: python encryption key rsa sha256