tomwpublic / hubitat_broadlink

Apache License 2.0
13 stars 2 forks source link

Input length must be multiple of 16 when decrypting with padded cipher #8

Closed albogban closed 1 year ago

albogban commented 2 years ago

Hello,

Function aes_cbc is trowing error Crypto.IllegalBlockSizeExcept. I found the solution to the problem. Please verify that it works in all instances. As far as for me, it works. See below:

`def aes_cbc(data, op, key, iv) { // thanks: https://community.hubitat.com/t/groovy-aes-encryption-driver/31556 try {

    def cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE")

    byte[] keyBytes = HexUtils.hexStringToByteArray(key)

    SecretKeySpec aKey = new SecretKeySpec(keyBytes, "AES")

    byte[] ivBytes = HexUtils.hexStringToByteArray(iv)

    IvParameterSpec aIv = new IvParameterSpec(ivBytes)

    cipher.init(op == "enc" ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, aKey, aIv)

    if (data instanceof List) { data = arrayListToByteArray(data) }

    if (data instanceof String) { data = data.getBytes() }

    byte[] original = cipher.doFinal(data)

    return original

} catch (e){

    log_debug("BroadLink Remote AES Encrypt: Caught Execption [${e}]")

}

return null

}`

tomwpublic commented 2 years ago

Are you seeing an invalid response from a broadlink device that leads to this error? Could you describe how to reproduce it (which device and operation)?

albogban commented 2 years ago

Hello,

Yes, I was using Broadlink. I purchased Remote Fan Control (RF type) (Link to the RC: https://a.co/d/4XkLqCW) , and during process of learning of RF code I was getting this type of error. So, it looks like code that has been picked up by Broadlink is awkward, and creating error. I used native Broadlink app to get Broadlink RM4 Pro IR RF 315MHz-433MHz (to learn codes from ceiling fan control (RF type) and it worked. So, I concluded that is the issue of encoding and decoding part as it was throwing the error that I mentioned in the previous communication.

On September 13, 2022 04:55:19 tomwpublic @.***> wrote: Are you seeing an invalid response from a broadlink device that leads to this error? Could you describe how to reproduce it (which device and operation)?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

tomwpublic commented 2 years ago

Thank you for that explanation. It's very helpful.

Does the decrypted code work correctly after this change to pad the raw value?

albogban commented 2 years ago

Yes, no error, all things working fine, including previously IR learned codes(prior to change of code) Nevertheless, need more testing to insure that I didn't created potential error in your code, and that will work with the previous saved codes prior to change encoding and decoding part of the driver.

Thanks for the app. I greatly appreciate what you did.

Have a wonderful day

Aleksandar Bogdanovic

On September 13, 2022 06:00:47 tomwpublic @.***> wrote: Thank you for that explanation. It's very helpful.

Does the decrypted code work correctly after this change to pad the raw value?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

tomwpublic commented 1 year ago

Fixed here: https://github.com/tomwpublic/hubitat_broadlink/pull/11