Giter Site home page Giter Site logo

Encryption padding issue about python-tuya HOT 9 CLOSED

clach04 avatar clach04 commented on August 13, 2024
Encryption padding issue

from python-tuya.

Comments (9)

clach04 avatar clach04 commented on August 13, 2024

@nijave

I was also running it an issue with _pad where is tries to concatenate bytes with a string and leads to an exception

I'm definitely not seeing this with either PyCrypto nor pyaes. We need more information.

Commit dae73ad - dumps the Python version, which may be relevant. I've seen odd byte/string behaviors with old versions of Python 3.x. Enable logging as for #1

from python-tuya.

nijave avatar nijave commented on August 13, 2024

See example error in home-assistant/core#11000 (comment)

That person appears to be using python35 on Windows. I'll do some more research into this and try to figure out if it's Python version specific or based on which crypto library is used

from python-tuya.

nijave avatar nijave commented on August 13, 2024

On my HA

Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/tasks.py", line 182, in _step
    result = coro.throw(exc)
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1031, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/switch/__init__.py", line 113, in async_handle_switch_service
    yield from switch.async_turn_on()
  File "/usr/lib/python3.6/asyncio/futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
    future.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/switch/tuya.py", line 83, in turn_on
    self._device.set_status(True, self._switchid)
  File "/config/deps/lib/python3.6/site-packages/pytuya/__init__.py", line 228, in set_status
    payload = self.generate_payload(command, dps_id=switch)
  File "/config/deps/lib/python3.6/site-packages/pytuya/__init__.py", line 163, in generate_payload
    json_payload = self.cipher.encrypt(json_payload)
  File "/config/deps/lib/python3.6/site-packages/pytuya/__init__.py", line 41, in encrypt
    raw = self._pad(raw)
  File "/config/deps/lib/python3.6/site-packages/pytuya/__init__.py", line 71, in _pad
    return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
TypeError: can't concat str to bytes

from python-tuya.

clach04 avatar clach04 commented on August 13, 2024

@nijave hopefully resolved in commit fb1c289

from python-tuya.

clach04 avatar clach04 commented on August 13, 2024

Turns out the reason I didn't see this was I had not ran the Python 3 and pycrypto/pycryptodome combination (only py3 and pyaes).

from python-tuya.

nijave avatar nijave commented on August 13, 2024

Just curious, any reason you're using your own padding methods instead of the ones packaged with pycrypto? See nijave@3c94c9d

from python-tuya.

clach04 avatar clach04 commented on August 13, 2024

I don't have that with the version of PyCrypto I have installed.

Python 2.7.13 |Anaconda 4.3.1 (32-bit)| (default, Dec 19 2016, 13:36:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import Crypto
>>> Crypto.__file__
'C:\\ProgramData\\Anaconda2\\lib\\site-packages\\Crypto\\__init__.pyc'
>>> Crypto.version_info
(2, 6, 1, 'final', 0)
>>> from Crypto.Util import Padding
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name Padding

Otherwise this would be much more elegant :-(

I took your branch and then merged with master into https://github.com/clach04/python-tuya/tree/3-encryption-padding - but it doesn't work for me.

Can you confirm the master branch now works for you? Then we can close this one down.

from python-tuya.

nijave avatar nijave commented on August 13, 2024

I'll go ahead and do some testing with pycrypto (the older one I think 2.6.1), pycryptodome (3.7.1 I think is what's on HA), and pyaes on both Python2 & 3 (on Windows/Cygwin). If I have time I'll do Linux, too, although I wouldn't imagine there are any issues there based on the changes. That should give us some better coverage

from python-tuya.

clach04 avatar clach04 commented on August 13, 2024

Resolved by pull request #9

from python-tuya.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.