0

I'm developing a simple secure data exchange between Server-Client and having some problems at the time of implementing AES.

I've already implemented the Shared Key exchange (with public key crypto) and it works fine. The idea in my head was (pseudocode):

SERVER
ciphertext = AES.encrypt(sharedKey,data)
send(ciphertext)

CLIENT
ciphertext = receive()
plaintext = AES.decrypt(sharedKey,ciphertext)

And voilà. When I tried to implement that, I first found that there was an IV. I first tried setting it to all zeros, like this:

self.cipher = AES.new(self.Kshared, AES.MODE_CFB, '0000000000000000')
while( there is data to send ):
    ciphertext = self.cipher.encrypt(data)
    self.sendData(ciphertext)

Then, in the Client:

cipher = AES.new(Ksecreta, AES.MODE_CFB,'0000000000000000')
while( there is data to receive ):
    plaintext = cipher.decrypt('0000000000000000'+data)[16:]

This works fine for the FIRST message, but not for the rest. I assume my problem might has something to do with the IV but I have no idea. Plus, the first implementation I found used a salt to generate another key and also a random IV but the problem is that the client has no idea of which salt/IV is the Server using. I guess you could send that via public key crypto but I first want a simple working AES crypto.

Thanks.

4

1 回答 1

0

对于您的解密代码,无需在密文前面加上 IV。你试过了plaintext = cipher.decrypt(data)吗?

以明文形式传输 IV 是安全的。因此,您可以随机生成它,然后将其与密文一起发送到通信之外。类似的东西self.sendData(iv + ciphertext),后来iv = data[:16]ciphertext = data[16:]

另一个需要考虑的常见问题是编码——一些传输格式不能很好地发送原始字节(可能包括 NULL 字符)。将密文编码为base64进行传输是很常见的。如果您需要,请查看base64.b64encodebase64.b64decode

于 2013-11-10T17:09:42.440 回答