How to import external modules to Decoder

Ok. I resolved my problem.
previously I used version 3.0 of aes-js library but there is several functions which are not available in ES5.

I use aes-js in version 2.1.0 like mentioned below:

Instead of import libraries I simply add code to Decode() function before my other lines of code.
https://raw.githubusercontent.com/ricmoo/aes-js/v2.1.0/index.js

It is working fine now.
I knew that is is possible :stuck_out_tongue:

Thank you for your help.

2 Likes

for w-mbus aes decode?

I can confirm that using the pure javascript AES implementation from GitHub - ricmoo/aes-js: A pure JavaScript implementation of the AES block cipher and all common modes of operation for node.js or web browsers. works. I had to remove the node stuff (package and brackets around code), but other than that: perfect!

The complete decoder is around 800 lines, but doesnโ€™t seem to bother the serer too much. Iโ€™ve implemented the decoder to read the AES-128 encryption key from a device variable. This makes it easy to set individual keys for each device.

Background: We have some water-meters sending data encrypted with AES-128 CBC (yes, on top of the LoRaWAN encryption). We would like to ALL decoding inside the Chirpstack.

Decoder looks something like this:

function Decode(fPort, bytes, variables)
{
    var ret = {}
    ret['fport'] = fPort
    ret['raw'] = String(bytes);
    ret['raw_length'] = bytes.length;

    if (fPort == 100)
    {
      	if (bytes.length == 48)
        {
            var mo = ModeOfOperation['cbc'];
            var key = hexToBytes(variables['encryption_key'])
            var iv =  [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
            var func = function() { return new mo(key, iv); }
            var decrypter = func();
            var ciphertext = createBuffer(bytes)
            var plaintext = decrypter.decrypt(ciphertext);
            ret['raw_hex_decrypted'] = bytesToHex(plaintext);

            ...