kdschlosser / samsungctl

Remote control Samsung televisions via a TCP/IP connection
MIT License
154 stars 33 forks source link

ValueError: non-hexadecimal number found in fromhex() arg at position 255 #72

Closed andreas-bulling closed 5 years ago

andreas-bulling commented 5 years ago
python3 -m samsungctl --host 192.168.178.60 --method encrypted                                                                          (master)
page: <?xml version="1.0" encoding="UTF-8"?><service xmlns="urn:dial-multiscreen-org:schemas:dial" xmlns:atom="http://www.w3.org/2005/Atom"><name>CloudPINPage</name><options allowStop="true"/><state>stopped</state><atom:link rel="run" href="run"/></service>
show_pin_page: http:///ws/apps/CloudPINPage/run
Please enter pin from tv: 3656
second_step_response: {"auth_data":"{\"auth_type\":\"SPC\",\"request_id\":\"0\",\"GeneratorClientHello\":\"010100000000000000009E000000063635343332312701692294C04B288DC4F6910CB09A3B25E521BC6E48AA64ED4269A35DC65B74313681F8BEABFEC246FD218D0BFE32D62936AA5C20ECC83BB5EF5A96BF2C6EEE33C17D8FC1AD78B6401DDA8895AE087C413BB9A008CD063A08BE9F5BC5E6410EC534952D51C3FB42B598B4819D0620D5DEA2737FC542ED89A7F83C73461D462AE905AF201001CDFD64FBCBA2E7DDE5E21F98E7FF0000000000\"}"}

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/andreas/Desktop/samsungctl/samsungctl/__main__.py", line 426, in <module>
    main()
  File "/Users/andreas/Desktop/samsungctl/samsungctl/__main__.py", line 338, in main
    with Remote(config) as remote:
  File "/Users/andreas/Desktop/samsungctl/samsungctl/remote.py", line 74, in __call__
    return RemoteWrapper(conf)
  File "/Users/andreas/Desktop/samsungctl/samsungctl/remote.py", line 72, in __init__
    self.open()
  File "/Users/andreas/Desktop/samsungctl/samsungctl/utils.py", line 24, in wrapper
    return func(*args, **kwargs)
  File "/Users/andreas/Desktop/samsungctl/samsungctl/remote_encrypted/__init__.py", line 155, in open
    output = self.hello_exchange(tv_pin)
  File "/Users/andreas/Desktop/samsungctl/samsungctl/utils.py", line 44, in wrapper
    result = func(*args, **kwargs)
  File "/Users/andreas/Desktop/samsungctl/samsungctl/remote_encrypted/__init__.py", line 321, in hello_exchange
    self.config.id
  File "/Users/andreas/Desktop/samsungctl/samsungctl/remote_encrypted/crypto.py", line 85, in parseClientHello
    secret = bytes.fromhex(hex(pow(bnPGx, bnPrivateKey, bnPrime)).rstrip("L").lstrip("0x"))
ValueError: non-hexadecimal number found in fromhex() arg at position 255
kdschlosser commented 5 years ago

in the crypto.py file add this

print(hex(pow(bnPGx, bnPrivateKey, bnPrime)))

before this line

secret = bytes.fromhex(hex(pow(bnPGx, bnPrivateKey, bnPrime)).rstrip("L").lstrip("0x"))

and paste the output for me please.

andreas-bulling commented 5 years ago

It only happens sometimes - now I instead always see https://github.com/kdschlosser/samsungctl/issues/73 again and can't reproduce the problem mentioned above. Will continue trying and paste the output when I hit that error again.

kdschlosser commented 5 years ago

OK I will fix #73 shortly.

andreas-bulling commented 5 years ago

now this error occurred again

kdschlosser commented 5 years ago

ok great. did you add that print statement?? do you have a printout???

p3g4asus commented 5 years ago

I know exactly what causes this random issue. It was an issue also in the original code. I made a fix for this and will be released when I will made a PR of my branch that adds the full compatibility with python 2. However this should fix it:

secret = hex(pow(bnPGx, bnPrivateKey, bnPrime)).rstrip("L").lstrip("0x")
secret = ((len(secret)%2)*'0')+secret
secret = bytes.fromhex(secret)

As I said this fix will be in my PR (slightly modified because, as it is, it is only python 3 compatible).

kdschlosser commented 5 years ago

@p3g4asus do a separate PR for this fix please. remember the ability to reverse changes. makes it easier with more commits to narrow down where an issue may be.

TY for coming up with a solution. Makes it easier when you have the TV..