mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-02-04 09:59:59 +00:00
Support Unicode strings in encrypt/decrypt (#125)
This commit is contained in:
committed by
Teemu R
parent
e612560b35
commit
a6d515b5cb
@@ -86,14 +86,16 @@ class TPLinkSmartHomeProtocol:
|
||||
:return: ciphertext request
|
||||
"""
|
||||
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR
|
||||
buffer = bytearray(struct.pack(">I", len(request)))
|
||||
|
||||
for char in request:
|
||||
cipher = key ^ ord(char)
|
||||
key = cipher
|
||||
buffer.append(cipher)
|
||||
plainbytes = request.encode()
|
||||
buffer = bytearray(struct.pack(">I", len(plainbytes)))
|
||||
|
||||
return buffer
|
||||
for plainbyte in plainbytes:
|
||||
cipherbyte = key ^ plainbyte
|
||||
key = cipherbyte
|
||||
buffer.append(cipherbyte)
|
||||
|
||||
return bytes(buffer)
|
||||
|
||||
@staticmethod
|
||||
def decrypt(ciphertext: bytes) -> str:
|
||||
@@ -106,13 +108,11 @@ class TPLinkSmartHomeProtocol:
|
||||
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR
|
||||
buffer = []
|
||||
|
||||
ciphertext_str = ciphertext.decode('latin-1')
|
||||
for cipherbyte in ciphertext:
|
||||
plainbyte = key ^ cipherbyte
|
||||
key = cipherbyte
|
||||
buffer.append(plainbyte)
|
||||
|
||||
for char in ciphertext_str:
|
||||
plain = key ^ ord(char)
|
||||
key = ord(char)
|
||||
buffer.append(chr(plain))
|
||||
plaintext = bytes(buffer)
|
||||
|
||||
plaintext = ''.join(buffer)
|
||||
|
||||
return plaintext
|
||||
return plaintext.decode()
|
||||
|
||||
Reference in New Issue
Block a user