Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Almost there... #1

Open
GrumpyMeow opened this issue Jun 24, 2018 · 1 comment
Open

Almost there... #1

GrumpyMeow opened this issue Jun 24, 2018 · 1 comment

Comments

@GrumpyMeow
Copy link

Thank you for sharing your information...
I think i've (almost) succesfully reverse engineered the decryption process..
Unfortunatly i still get a 401 when posting the decrypted password.
Maybe you have more success.

import binascii
import logging
import json
import requests
from requests.auth import HTTPDigestAuth
import voluptuous as vol

from passlib.utils import ab64_encode, ab64_decode
from passlib.hash import pbkdf2_sha1
import binascii
from Crypto.Cipher import AES
import os

def confirm_connection(hass, host, username, password, salt, pin):
hash = pbkdf2_sha1.encrypt(pin, salt = salt.encode('utf-8'), salt_size = 128, rounds = 1331)
hexhash = (binascii.b2a_hex(ab64_decode(hash.split('$')[-1].encode())).decode())
hash = binascii.unhexlify(hexhash[:32])

iv = bytes([0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0])
cipher = AES.new(hash, AES.MODE_CBC, iv)
decrypted_text = cipher.decrypt( binascii.unhexlify(password) )
decrypted_hex = binascii.hexlify(decrypted_text)[:64]
decrypted_text = binascii.unhexlify(decrypted_hex)

hass.states.set('toshibatv.password2', decrypted_hex)

_LOGGER.info('Post confirm_connection with %s %s', username, decrypted_text)


url = "http://" + host + ":" + PORT + POST_CONFIRM_CONNECTION
response = requests.post(url, auth=HTTPDigestAuth(username, decrypted_text), data='')
if response.status_code != 200:
        _LOGGER.exception("Failed to confirm_connection: %d", response.status_code)
        return False

status = response.json()['status']
if status != 0:
    _LOGGER.exception('Status: %d', status)

return True
@DSchndr
Copy link
Owner

DSchndr commented Jul 10, 2018

Maybe there is something wrong with decryption in your code, because PKCS5Padding is missing, which is somehow used in decryption in toshibas code.

//  From jp.co.toshiba.ome.android.RZTagler.tag.PinCheck:

deriveKey = PBKDF2.deriveKey(str.getBytes(), saltString.getBytes(), 1331, 16); //Derive key with pincode and salt and 1331, 16 bytes long
// PBKDF2.deriveKey(pincode, salt, 1331, 16)

Key secretKeySpec = new SecretKeySpec(deriveKey, "AES"); //
AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(this.iv); //

instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
instance.init(2, secretKeySpec, ivParameterSpec);
str = new String(instance.doFinal(hexToByte(encryptedPassword)), "UTF-8");
return str;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants