The two ciphers Baichuan uses for message bodies.

  • xorCipher: the fixed-key XOR scramble used before login, for the nonce reply and the login request itself.
  • aesCfbEncrypt / aesCfbDecrypt: AES-128-CFB with 128-bit segments and the fixed IV "0123456789abcdef", used once logged in. The key comes from the login nonce, see @hertzg/reolink-api/protocol/login.

Examples

Scramble and unscramble a body with the XOR cipher

import { assertEquals } from "@std/assert";
import { xorCipher } from "@hertzg/reolink-api/encoding/cipher";

const plain = new TextEncoder().encode("<?xml");
const scrambled = xorCipher(plain, 250);

assertEquals(scrambled, Uint8Array.of(0xfa, 0x8e, 0xd8, 0xfe, 0xee));
assertEquals(xorCipher(scrambled, 250), plain);

Functions

f
aesCfbDecrypt(
key: Uint8Array,
data: Uint8Array
): Uint8Array

Decrypts a body encrypted with AES-128-CFB, 128-bit segments, IV "0123456789abcdef".

f
aesCfbEncrypt(
key: Uint8Array,
data: Uint8Array
): Uint8Array

Encrypts a body with AES-128-CFB, 128-bit segments, IV "0123456789abcdef".

f
xorCipher(
data: Uint8Array,
offset: number
): Uint8Array

Applies the Baichuan XOR cipher. The cipher is its own inverse, so the same call encrypts and decrypts.