//TID号加密解密算法描述 //466fe489d=>70537421 const szKey3 = [0x35, 0x41, 0x32, 0x42, 0x33, 0x43, 0x36, 0x44, 0x39, 0x45, 0x38, 0x46, 0x37, 0x34, 0x31, 0x30 ]; //解密 ,参数16进制 const encrypt = (number) => { console.log(number) number = number.toUpperCase() console.log(number) let out_number = ''; if (!number) return; let len = number.length; console.log(len) if (len > 16) return; for (let i = 0; i < len; i++) { for (let j = 0; j < 16; j++) { if (number.charAt(i).charCodeAt() == szKey3[j]) //字符转ascii码 out_number += String.fromCharCode(0x2A + j); else continue; } } console.log(out_number) return out_number } // ArrayBuffer转16进度字符串示例 const ab2hex = (buffer) => { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } //字符串平均切割成数组 const strAverage2Arr = (str, no) => { let arr = []; for (let i = 0; i < str.length; i += no) { arr.push(str.slice(i, i + 2)); } return arr; } //数组的每个元素加前缀 const addFlagBeforeArr = (arr) => { return Array.prototype.map.call(arr, function (value) { return `0x${value}`; }) } //字符转ArrayBuffer const hexStringToArrayBuffer = (str) => { // 将16进制转化为ArrayBuffer return new Uint8Array(str.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })).buffer } const CRC = (dataBuf) =>{ var temp = 0; var crc = 0xffff; for (var i = 0; i < dataBuf.length; i++) { crc ^= parseInt(dataBuf[i].toString()); for (var j = 0; j < 8; j++) { temp = 1 & crc; crc >>= 1; if (temp == 1) { crc ^= 0xa001; } } } crc ^= 0xffff; crc = ('0000' + crc.toString(16)).substr(-4); return crc; } module.exports = { encrypt: encrypt, ab2hex: ab2hex, strAverage2Arr: strAverage2Arr, addFlagBeforeArr: addFlagBeforeArr, hexStringToArrayBuffer: hexStringToArrayBuffer, CRC:CRC }