1. Home
  2. Software
  3. Decrypt with Crypto-JS for HomeMatic

Decrypt with Crypto-JS for HomeMatic

The following flow shows the decryption of data from the air-Q which should be easy to integrate into more complex scenarios.

First of all crypto-js must be installed

npm install crypto-js

The http request node must be set to GET and the URL or IP address of the air-Q must be specified as an http call with the route /data. The result is passed directly as parsed JSON.

In the settings.js file in the node-red directory, the entry

functionExternalModules: false

to

functionExternalModules: true

must be set and node-red restarted. Now there is a new option for the function-Node the possibility to include external modules. Here crypto-js is to be inserted as CryptoJS. In the case of the latter, the correct capitalization of the corresponding letters must be ensured.

Now, as a function in the function node, the JS example from the air-Q doc can be used almost 1:1.

The password must of course be replaced by the set of the air-Q.
Here again for Copy&Paste:

let airqpass = 'airqsetup'

function decrypt(msgb64, airqpass) {
    if (airqpass.length < 32) {
      for (i=airqpass.length; i<32; i++) {
        airqpass += "0";
      }
    } else if (airqpass.length > 32) {
      airqpass = airqpass.substring(0,32)
    }
    var key = CryptoJS.enc.Utf8.parse(airqpass);
    var ciphertext = CryptoJS.enc.Base64.parse(msgb64);
    var iv = ciphertext.clone();
    iv.sigBytes = 16;
    iv.clamp();
    ciphertext.words.splice(0, 4); // delete 4 words = 16 bytes
    ciphertext.sigBytes -= 16;
    var decrypted = CryptoJS.AES.decrypt({ciphertext: ciphertext}, key, {
        iv: iv
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
  }

decryptedMsg = decrypt(msg.payload.content, airqpass);
newMsg = { payload: decryptedMsg };
return newMsg;

Afterwards, the result is sent through the JSON parser and is available for further processing as a JSON object.

Was this article helpful?

Related Articles