一、加密
1.首先假設你已經將Excel表格數據導出為了json數據
2.然后可以通關nodejs對其進行xor加密
const fs = require('fs');// 讀取配置文件
const path = "hero_info.json";
const data = fs.readFileSync(path, 'utf-8');
const jsonObject = JSON.parse(data);// XOR 加密函數
function xorEncrypt(buffer, key) {const encrypted = Buffer.alloc(buffer.length);console.log(encrypted);for (let i = 0; i < buffer.length; i++) {encrypted[i] = buffer[i] ^ key; // 對每個字節與密鑰進行異或}return encrypted;
}// 將 JSON 對象轉換為字符串
const jsonString = JSON.stringify(jsonObject);// 使用 Buffer 將字符串轉換為二進制數據
const binaryData = Buffer.from(jsonString, 'utf-8');// 定義一個密鑰(簡單示例,使用一個字節)
const encryptionKey = 0x55; // 0x55 是一個簡單的密鑰// 對二進制數據進行加密
const encryptedData = xorEncrypt(binaryData, encryptionKey);// 將加密后的二進制數據寫入文件
fs.writeFileSync('hero_info.bin', encryptedData);
二、解密
protected onLoad(): void {this.loadConfig();
}// 加載配置文件hero_info
loadConfig() {const encryptionKey = 0x55;let path = "hero_info";resources.load(path, (err, buffAsset: BufferAsset) => {const arrayBuff = buffAsset.buffer();const decAB = this.xorDecrypt(arrayBuff, encryptionKey);let decoder = new TextDecoder('utf-8');let decodedString = decoder.decode(decAB);console.log("data:", decodedString);});
}
// 異或解密
xorDecrypt(buffer, key) {return this.xorEncrypt(buffer, key); // 解密和加密是一樣的操作
}
// 異或加密
xorEncrypt(buffer: ArrayBuffer, key: number) {const data = new Uint8Array(buffer);const out = new Uint8Array(data.length);for (let index = 0; index < data.length; index++) {out[index] = data[index] ^ key; }return out;
}