【问题标题】:Converting Python 2 to 3 with Encryption Function使用加密函数将 Python 2 转换为 3
【发布时间】:2019-07-29 00:45:17
【问题描述】:

该脚本是为 Python 2 编写的,但我需要将其转换为 Python 3。当我这样做时,它会抛出此错误“TypeError: can't concat str to bytes”

结果:

Traceback (most recent call last):
   File "tplink_smartplug.py", line 105, in <module>
       sock_tcp.send(encrypt(cmd))
   File "tplink_smartplug.py", line 70, in encrypt
       result += chr(a)

TypeError: can't concat str to bytes

# 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)   #line70
    return result

def decrypt(string):
    key = 171
    result = ""
    for i in string:
        a = key ^ ord(i)
        key = ord(i)
        result += chr(a)
    return result

# Send command and receive reply
try:
    sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_tcp.connect((ip, port))
    sock_tcp.send(encrypt(cmd))    #line105
    data = sock_tcp.recv(2048)
    sock_tcp.close()

    print(("Sent:     ", cmd ))
    print(("Received: ", decrypt(data[4:]) ))
except socket.error:
    quit("Cound not connect to host " + ip + ":" + str(port))

【问题讨论】:

  • 试试result += chr(a).encode()result += bytes( [a] )

标签: python encryption cmd tcp decode


【解决方案1】:

是的,它已在GITHUBGITHUB 上针对 Python3 进行了修订

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 2023-01-10
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多