title: Python - Data Sent Over Socket Appears Different on Client and Server
body: <p>I've got a client/server program where the client sends plaintext to the server which then runs AES encryption and returns the ciphertext. I'm using the following algorithm for the encryption/decryption:</p>
  
  <p><a href="http://anh.cs.luc.edu/331/code/aes.py" rel="nofollow">http://anh.cs.luc.edu/331/code/aes.py</a></p>
  
  <p>When I get the results back from the encryption and print them on the server-side I see mostly gibberish in the terminal. I can save to a file immediately and get something along these lines:</p>
  
  <pre><code>tgâY†Äô®Ø8Î¯6ƒlÑÝ%ŠIç°´&gt;§À¥0Ð
  </code></pre>
  
  <p>I can see that this is the correct output because if I immediately decrypt it on the server, I get the original plaintext back. If I run this through the socket, send it back to the client, and print(), I get something more like this:</p>
  
  <pre><code>\rtg\xe2Y\x86\x8f\xc4\xf4\xae\xd88\xce\xaf6\x83l\xd1\xdd%\x8aI\xe7\xb0\xb4&gt;\xa7\xc0\x18\xa50\xd0
  </code></pre>
  
  <p>There's an obvious difference here. I'm aware that the \x represents a hex value. If I save on the client-side, the resulting text file still contains all \x instances (i.e., it looks exactly like what I displayed directly above). What must I do to convert this into the same kind of output that I'm seeing in the first example? From what I have seen so far, it seems that this is unicode and I'm having trouble...</p>
  
  <p>Relevant code from server.py</p>
  
  <pre><code>key = aes.generateRandomKey(keysizes[len(key)%3])
  encryptedText = aes.encryptData(key, text)
  f = open("serverTest.txt", "w")
  f.write(encryptedText)
  f.close()
  print(encryptedText)
  decryptedText = aes.decryptData(key, encryptedText)
  print(decryptedText)
  conn.sendall(encryptedText)
  </code></pre>
  
  <p>Relevant code from client.py</p>
  
  <pre><code>cipherText = repr(s.recv(16384))[1:-1]
  s.close()
  cipherFile = raw_input("Enter the filename to save the ciphertext: ")
  print(cipherText)
  f = open(cipherFile, "w")
  f.write(cipherText)
  </code></pre>
  
  <p>Edit: To put this simply, I need to be able to send that data to the client and have it display in the same way as it shows up on the server. I feel like there's something I can do with decoding, but everything I've tried so far doesn't work. Ultimately, I'll have to send from the client back to the server, so I'm sure the fix here will also work for that, assuming I can read it from the file correctly.</p>
  
  <p>Edit2: When sending normally (as in my code above) and then decoding on the client-side with "string-escape", I'm getting identical output to the terminal on both ends. The file output also appears to be the same. This issue is close to being resolved, assuming I can read this in and get the correct data sent back to the server for decrypting.</p>