我正在做一个允许人们交换加密信息的项目。在
我相信问题出在我传输加密信息的时候。我向这个函数传递一个字符串(消息)
def sendInfo(host, port, sendObject):
socit = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("#Connecting")
socit.connect((host, port))
print("#Sending")
#sendObject = unicode(sendObject, 'utf-8')
#bites = str.encode(sendObject)
bites = bytes(sendObject)
print str(bites)
#bites = pickle.dumps(sendObject)
socit.send(bites)
socit.close()
print("Sent successfully")
我发出的传递信息的一个例子是:
^{pr2}$然后我将其转换为utf-8可接受的:
^{3}$我用来解密的代码基于pyrsa库(http://stuvel.eu/rsa):
def decrypt(message, privKey):
return rsa.decrypt(message, privacy)
我得出的错误是:
Traceback (most recent call last):
File "/Users/Andrew/Shatter/ShatterListen.py", line 142, in <module>
main()
File "/Users/Andrew/Shatter/ShatterListen.py", line 127, in main
ciphertext = ShatterRSA.decrypt(ciphertext, listenerKey[1])
File "/Users/Andrew/Shatter/ShatterRSA.py", line 85, in decrypt
return rsa.decrypt(message, privKey)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rsa/pkcs1.py", line 232, in decrypt
raise DecryptionError('Decryption failed')
DecryptionError: Decryption failed
如果有人能对情况有所了解,并对可能出现的问题提出一些建议。我90%肯定传输的字符串没有被正确解码,这就是错误的来源,但我不知道我可以尝试什么。如果你需要更多的代码或信息,我很乐意提供。在
编辑:
以下是一些代码,显示了单个模块中的加密过程,但未被传输:
key = create_key(1024)
ciphertext = encrypt('Hey there!', key[0])
print ciphertext
print decrypt(ciphertext, key[1])
其输出为:
>>>
Hey there!
>>>
字节字符串
"(x02\xc6\x07\xa5\xb1\xc4t ..."
不是有效的UTF-8流。在以位110
开头的\xc6
之后,下一个字节必须以10
开头,\x07
则不是。可以在the Wikipedia article中找到编码工作原理的良好摘要。在你说你把收到的密文“转换”成这个字符串;我想这是通过一个RSA解密步骤?如果是这样,那么有几件事需要检查:
相关问题 更多 >
编程相关推荐