当装甲串是PGPY的不介绍时,如何使用PGPY解密PGP装甲弦?
我正在创建一个脚本,该脚本与使用PGP加密来加密其响应的服务器进行通信。
我可以请求服务器的公钥,并使用该密钥通过PGPY库加密我的请求,并且服务器能够解密并响应。
脚本加密的请求中包含其公共密钥,服务器期望它是其API的一部分,因此它采用该密钥并加密响应并将其发送回我的脚本。我的脚本收到了这种加密的响应,但是PGPY似乎缺乏解密它的设施。
据我所知,PGPY只能解密其加密的消息,因为pgpkey.decrypt()
got pgpmessage
对象,而不是装甲字符串,而我不能找到任何采用装甲串并解密它或将其转换为pgpmessage
的方法,以便我可以解密。
就我而言,这是:
def get_token_from_payload(encrypted_payload):
print("Payload: ", encrypted_payload)
privkey, _ = pgpy.PGPKey.from_file('private.key')
message_blob = pgpy.PGPMessage.new(encrypted_payload)
token_string = privkey.decrypt(message_blob).message
print("Token: ", token_string)
return json.load(token_string)
message_blob = pgpy.pgpmessage.new(encrypted_payload)
我认为会做正确的事情,不,而是使它自己的加密文本blob行token_string = privkey.decrypt(message_blob).message
简单地转回我开始使用的装甲ASCII字符串。当然,最后一行之所以失败,是因为没有JSON
可以做任何事情,因此我们现在就忽略了这一点,因为PGPY解密事物后应该可以正常工作。
我一定缺少一些东西。如果无法执行此操作,则该库本质上是没有用的。如何解密装甲串,或者将装甲串转换为pgpmessage
,然后可以解密?
更新:
def get_token_from_payload(encrypted_payload):
ep_file = open("ep.pgp", "w")
ep_file.write(encrypted_payload)
ep_file.close()
privkey, _ = pgpy.PGPKey.from_file('private.key')
message = pgpy.PGPMessage.from_file("ep.pgp")
token_string = privkey.decrypt(message).message
return json.loads(token_string)
此解决方案确实有效,但是我认为它不可接受,因为它需要一组无关的IO步骤,创建文件然后直接读取。直接解密它。
I'm creating a script that communicates with a server that is using PGP encryption to encrypt it's responses.
I can request the public key of the server, and use that key to encrypt my request via the PGPy library, and the server is able to decrypt it and respond.
The scripts encrypted request has it's public key in it, which the server is expecting as a part of it's API, so it takes that key and encrypts the response and sends it back to my script. My script receives this encrypted response fine, but PGPy appears to lack the facilities to decrypt it.
From what I can tell, PGPy can only decrypt a message that it encrypted because the the PGPKey.decrypt()
takes a PGPMessage
object, not an armored string, and I cannot find any method that takes an armored string and either decrypts it or turns it into a PGPMessage
so that I can decrypt it.
This is as far as I've gotten:
def get_token_from_payload(encrypted_payload):
print("Payload: ", encrypted_payload)
privkey, _ = pgpy.PGPKey.from_file('private.key')
message_blob = pgpy.PGPMessage.new(encrypted_payload)
token_string = privkey.decrypt(message_blob).message
print("Token: ", token_string)
return json.load(token_string)
The message_blob = pgpy.PGPMessage.new(encrypted_payload)
that I thought would do the right thing, doesn't, and instead makes it's own encrypted text blob that the line token_string = privkey.decrypt(message_blob).message
simply turns back into the armored ascii string I started with. Of course the last line fails because there is no json
to do anything with so we'll ignore that for right now as that should work just fine once PGPy is decrypting things.
I must be missing something. This library is essentially useless if it can't do this. How do I either decrypt the armored string or convert the armored string into a PGPMessage
that can then be decrypted?
Update:
def get_token_from_payload(encrypted_payload):
ep_file = open("ep.pgp", "w")
ep_file.write(encrypted_payload)
ep_file.close()
privkey, _ = pgpy.PGPKey.from_file('private.key')
message = pgpy.PGPMessage.from_file("ep.pgp")
token_string = privkey.decrypt(message).message
return json.loads(token_string)
This solution does work, but I don't find it acceptable as it requires an extraneous set of IO steps, creating a file and then reading it right back in. I already have the armored ascii message in memory, I just want to decrypt it directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够通过使用pgpy.pgpmessage.from_blob()从内存中的ASCII装甲字符串中创建一个PGPMESSAGE对象,
您可能还需要将令牌值从ytearray转换为字符串。这样的事情应该有效:
You should be able to create a PGPMessage object from the ascii armored string in memory by using pgpy.PGPMessage.from_blob()
You might also need to convert the token value from a bytearray to a string. Something like this should work: