Python - Pycrypto - 通过网络发送加密数据
我试图让两个程序使用公钥在网络上共享加密数据,但我遇到了一个难题:共享的信息(密钥和/或加密数据)似乎被修改了。我希望保持加密数据格式和密钥格式尽可能简单,以便与其他语言兼容。 为了解决这个问题,我创建了 2 个程序:Keyreceive 和 Keysend。 它们按以下顺序执行:
- Keyreceive 启动并等待接收加密数据
- Keysend 启动并生成 RSA 密钥,将导出的私钥保存到文件中
- Keysend 加密一段数据并将其通过网络发送到 Keyreceive
- Keyreceive 导入来自同一文件的私钥,并使用它来解密加密数据
- Keysend 还解密加密数据以验证结果
Keysend.py
import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random
rng = Random.new().read
RSAkey = RSA.generate(1024, rng)
privatekey = RSAkey
publickey = RSAkey.publickey()
print(privatekey.exportKey()) #export under the 'PEM' format (I think)
print(publickey.exportKey())
file = open("Keys.txt", "w")
file.write(privatekey.exportKey()) #save exported private key
file.close()
data = "hello world"
enc_data = publickey.encrypt(data, 16) #encrypt message with public key
print(str(enc_data))
host = "localhost"
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.connect((host, port))
connexion.send(str(enc_data)) # send encrypted data, this appears to be the source of the problem
dec_data = RSAkey.decrypt(enc_data) # test decryption
print(dec_data)
os.system("pause")
Keyreceive.py
import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random
host = ''
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.bind((host, port))
connexion.listen(5)
clientconnexion, connexioninfo = connexion.accept()
enc_data = clientconnexion.recv(1024) # receive encrypted data
print(enc_data)
file = open("Keys.txt", "r")
privatestr = file.read() # retrieve exported private key from file
file.close()
print(privatestr)
privatekey = RSA.importKey(privatestr) # import private key
data = privatekey.decrypt(enc_data) # decrypt sent encrypted data
print(data)
os.system("pause")
在两个文件都完成解密加密数据后,Keysender 输出原始消息:“hello world”,而 Keyreceiver 输出乱码。 如果加密数据和密钥格式中存在“隐藏”信息,是否有某种方法可以将它们以“纯”文本格式写入?
I am trying to get 2 programs to share encrypted data over a network using public keys, but I am stuck with a difficult problem : the information that is shared (the keys and/or the encrypted data) seems to get modified. I am hoping to keep the encrypted data format as well as the format of the keys as simple as possible in order to allow compatibility with other languages.
To break down the problem, I have created 2 programs : Keyreceive and Keysend.
They execute in this order :
- Keyreceive starts up and waits to receive the encrypted data
- Keysend starts up and generates an RSA key, saving the exported private key to a file
- Keysend encrypts a piece of data and sends it to Keyreceive over the network
- Keyreceive imports the private key from the same file, and uses it to decrypt the encrypted data
- Keysend also decrypts the encrypted data to verify the result
Keysend.py
import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random
rng = Random.new().read
RSAkey = RSA.generate(1024, rng)
privatekey = RSAkey
publickey = RSAkey.publickey()
print(privatekey.exportKey()) #export under the 'PEM' format (I think)
print(publickey.exportKey())
file = open("Keys.txt", "w")
file.write(privatekey.exportKey()) #save exported private key
file.close()
data = "hello world"
enc_data = publickey.encrypt(data, 16) #encrypt message with public key
print(str(enc_data))
host = "localhost"
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.connect((host, port))
connexion.send(str(enc_data)) # send encrypted data, this appears to be the source of the problem
dec_data = RSAkey.decrypt(enc_data) # test decryption
print(dec_data)
os.system("pause")
Keyreceive.py
import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random
host = ''
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.bind((host, port))
connexion.listen(5)
clientconnexion, connexioninfo = connexion.accept()
enc_data = clientconnexion.recv(1024) # receive encrypted data
print(enc_data)
file = open("Keys.txt", "r")
privatestr = file.read() # retrieve exported private key from file
file.close()
print(privatestr)
privatekey = RSA.importKey(privatestr) # import private key
data = privatekey.decrypt(enc_data) # decrypt sent encrypted data
print(data)
os.system("pause")
After both files have finished decrypting the encrypted data, Keysender outputs the original message : "hello world", whereas Keyreceiver outputs gibberish.
If there is "hidden" information in the encrypted data and key formats, would there be some way of writing them in a "pure" text format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对问题根源所在的行是正确的。
这里的 enc_data 是一个元组,其第一个(实际上也是唯一的)元素是包含实际密文的字符串。当您对其调用
str
时,Python 会尝试将元组转换为字符串,这不是您想要的。如果你把它改成这样:那么它应该做你想要的。
You're right about which line is the source of the problem.
enc_data
here is a tuple, the first (and in fact only) element of which is a string containing the actual ciphertext. When you are callingstr
on it, you're getting Python's attempt to convert the tuple to a string, which is not what you want. If you change it to this:then it should do what you want.