Search code examples
python-3.xsocketsutf-8binaryaes

How to ingnore \n in binary until actual new line marker appears


I have some text that I want to encode. Once it is encoded, it has "\n" in it. I then have python add b'\n' to add a new line. My socket thinks the first \n is the end of the file when it is actually part of the text.

I have looked through my code and this is just something I don't know how to fix.

Generating The Binary Data

print("Calculating Key")
sharedKey = str((int(recieverPublicKey) ** int(privateKey)) % int(mod))
sharedKey = hashlib.sha256(sharedKey.encode())
sharedKey = sharedKey.hexdigest()
sharedKey = sharedKey[0:32]
print("Encrypting Key")
initVector = 16 * '\x00'
sharedKey = bytes(str(sharedKey), encoding='utf-8')
encryptor = AES.new(sharedKey, AES.MODE_CBC, initVector)
encryptedKey = encryptor.encrypt(dencryptionKey)
print(encryptedKey)
time.sleep(0.1)
print("Sending Encryption Key")
sock.sendall(encryptedKey + b'\n')

Decoding

sharedKey = str((int(senderPublicKey ** int(privateKey))) % int(mod))
sharedKey = hashlib.sha256(sharedKey.encode())
sharedKey = sharedKey.hexdigest()
sharedKey = sharedKey[0:32]
initVector = 16 * '\x00'
print("Recieving Encryption Key")
time.sleep(0.1)
print("Decrypting Key")
encryptedKey = clientFile.readline()
print(encryptedKey)
encryptedKey = encryptedKey.strip().decode()
print(encryptedKey)
initVector = 16 * '\x00'
sharedKey = bytes(str(sharedKey), encoding='utf-8')
decryptor = AES.new(sharedKey, AES.MODE_CBC, initVector)
encryptedKey = encryptedKey[2:-2]
print(encryptedKey)
for x in range(8):
    encryptedKeyPart = encryptedKey[0:16]
    print(encryptedKey)
    encryptedKey = encryptedKey[16:-1]
    dencryptionKey = dencryptionKey +    decryptor.decrypt(encryptedKeyPart).decode('utf-8')
    print(dencryptionKey)

Output on recieving side

b';l\xb8\xd7{\xa7Wt\x99\xb6\xc2\xfa\xf8\x8c\xe6\xd7=\x1d\x8e\x0b\r\x8e\xa4\xee\xa3\x84\xca\xfd\x91L\xda\xbb\x82\xc3\x93\x0f\xec\xd9o\xa7V\x1fs\x89{\x87\xd3\xf0\xc3\xba\x8f\xd9\xd0v\x18k-\xd9\xb1C&Y\x82+\xb8\xb6\x85:\xfd\x9f\x13\xea\xd8v\xadG\x11_\xbf4r\xb6\xa3\n'

Output on sending side

b';l\xb8\xd7{\xa7Wt\x99\xb6\xc2\xfa\xf8\x8c\xe6\xd7=\x1d\x8e\x0b\r\x8e\xa4\xee\xa3\x84\xca\xfd\x91L\xda\xbb\x82\xc3\x93\x0f\xec\xd9o\xa7V\x1fs\x89{\x87\xd3\xf0\xc3\xba\x8f\xd9\xd0v\x18k-\xd9\xb1C&Y\x82+\xb8\xb6\x85:\xfd\x9f\x13\xea\xd8v\xadG\x11_\xbf4r\xb6\xa3\ng\xb0\xde\xa9jW\x8d\x85T\x06mR\x0e\xfe\xcar\xaa\x17\x98j\x92lV\x07\xf2}\xad%\x0c\x01\xe4\xf1\xd7=\x8a\xa7\xeb\xd2\xb2c\xb8\x88\xb0\x9b\n'

I wanted it to receive all the data and start decrypting the text. The programs stops at a \n which is supposed to be part of the text and doesn't read the rest causing .decode('utf-8') to throw an error because it is unfinished.


Solution

  • If the data you are sending contains a newline, you can't use a newline as a delimiter to the message. As in your other question and my answer, send the size of the encryption key and a newline, then the encryption key. On the receiving side, receive the size of the encryption key terminated by the newline, then read exactly that number of bytes for the key itself.