From 2ab78544aebb0f22900244be54499a86535d16d6 Mon Sep 17 00:00:00 2001 From: Samuel Tseng Date: Wed, 19 Feb 2020 23:56:55 -0800 Subject: [PATCH 1/2] Refactored code for py3 while keeping py2 support (not using future) --- tplink_smartplug.py | 58 ++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 4012a55..1b1f55c 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -19,6 +19,7 @@ # limitations under the License. # +import sys import socket import argparse from struct import pack @@ -51,23 +52,43 @@ commands = {'info' : '{"system":{"get_sysinfo":{}}}', # Encryption and Decryption of TP-Link Smart Home Protocol # XOR Autokey Cipher with starting key = 171 -def encrypt(string): - key = 171 - result = pack('>I', len(string)) - for i in string: - a = key ^ ord(i) - key = a - result += chr(a) - return result +if sys.version_info[0] > 2: + def encrypt(string): + key = 171 + result = pack('>I', len(string)) + for i in string: + a = key ^ ord(i) + key = a + result += bytes([a]) + return result -def decrypt(string): - key = 171 - result = "" - for i in string: - a = key ^ ord(i) - key = ord(i) - result += chr(a) - return result + def decrypt(string): + key = 171 + result = "" + for i in string: + a = key ^ i + key = i + result += chr(a) + return result + +else: + def encrypt(string): + key = 171 + result = pack('>I', len(string)) + for i in string: + a = key ^ ord(i) + key = a + result += chr(a) + return result + + def decrypt(string): + key = 171 + result = "" + for i in string: + a = key ^ ord(i) + key = ord(i) + result += chr(a) + return result # Parse commandline arguments parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) @@ -96,8 +117,7 @@ try: data = sock_tcp.recv(2048) sock_tcp.close() - print "Sent: ", cmd - print "Received: ", decrypt(data[4:]) + print("Sent: ", cmd) + print("Received: ", decrypt(data[4:])) except socket.error: quit("Cound not connect to host " + ip + ":" + str(port)) - From cef33b842529305484015057c6a7eb693a7d8b08 Mon Sep 17 00:00:00 2001 From: Samuel Tseng Date: Thu, 20 Feb 2020 00:04:56 -0800 Subject: [PATCH 2/2] Added Comments to easily identify code sections --- tplink_smartplug.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 1b1f55c..11c3d3b 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -52,6 +52,7 @@ commands = {'info' : '{"system":{"get_sysinfo":{}}}', # Encryption and Decryption of TP-Link Smart Home Protocol # XOR Autokey Cipher with starting key = 171 +# Python 3.x Version if sys.version_info[0] > 2: def encrypt(string): key = 171 @@ -71,6 +72,7 @@ if sys.version_info[0] > 2: result += chr(a) return result +# Python 2.x Version else: def encrypt(string): key = 171