@jstark/tplink-smarthome-crypto
v6.0.2
Published
Encryption/decryption for TP-Link Smart Home device communication
Maintainers
Readme
@jstark/tplink-smarthome-crypto
Encryption/decryption for TP-Link Smart Home device communication — a maintained, dependency-free fork of plasticrake/tplink-smarthome-crypto. Requires Node.js 22+.
TP-Link devices use a simple XOR "autokey" cipher. TCP messages are prefixed with a 4-byte big-endian length header; UDP messages are not.
Install
npm install @jstark/tplink-smarthome-cryptoUsage
const {
encrypt,
decrypt,
encryptWithHeader,
decryptWithHeader,
} = require('@jstark/tplink-smarthome-crypto');
const payload = '{"system":{"get_sysinfo":{}}}';
// UDP (no length header)
const udp = encrypt(payload);
decrypt(udp).toString(); // -> original payload
// TCP (4-byte length header)
const tcp = encryptWithHeader(payload);
decryptWithHeader(tcp).toString(); // -> original payloadAPI
All functions accept a Buffer or string and return a Buffer.
encrypt(input, firstKey = 0xab)— encrypt without a length header (UDP).decrypt(input, firstKey = 0xab)— decrypt data without a length header (UDP).encryptWithHeader(input, firstKey = 0xab)— encrypt and prepend a 4-byte length header (TCP).decryptWithHeader(input, firstKey = 0xab)— strip the 4-byte header and decrypt (TCP).
Supported Devices
Smart plugs/switches (HS100, HS200, HSxxx, KPxxx) and smart bulbs (LB100, LB200, LBxxx, KLxxx). Note that Tapo devices are not supported.
Related Projects
- @jstark/tplink-smarthome-api — the smart home API that uses this library (same monorepo)
- TP-Link Smarthome Device Simulator — useful for automated testing
- TP-Link Smarthome Homebridge Plugin
Credits
Forked from plasticrake/tplink-smarthome-crypto (MIT). Thanks to George Georgovassilis and Thomas Baust for figuring out the HS1XX encryption.
