前言
变体选择符是一个基本多文种平面的 Unicode区段,包括 16 个变体选择符。这些选择器用于描述前一个字符的特点字形。(维基百科)
JS通过Unicode变体选择符(Variation Selector, VS)实现隐写术
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| const VS16_START = 65024; const VS_EXT_START = 917760;
function encode(plainText) { if (typeof plainText !== "string") { throw new TypeError("编码入参必须为字符串类型"); }
const encoder = new TextEncoder(); const bytes = encoder.encode(plainText);
let secretStr = ""; for (const byte of bytes) { if (byte < 16) { secretStr += String.fromCodePoint(VS16_START + byte); } else { secretStr += String.fromCodePoint(VS_EXT_START + (byte - 16)); } }
return secretStr; }
function decode(secretText) { if (typeof secretText !== "string") { throw new TypeError("解码入参必须为字符串类型"); }
const byteBuffer = []; const decoder = new TextDecoder("utf-8");
for (const char of [...secretText]) { const codePoint = char.codePointAt(0); if (codePoint >= VS16_START && codePoint <= VS16_START + 15) { byteBuffer.push(codePoint - VS16_START); } else if (codePoint >= VS_EXT_START && codePoint <= VS_EXT_START + 239) { byteBuffer.push(codePoint - VS_EXT_START + 16); } }
const uint8Array = new Uint8Array(byteBuffer); return decoder.decode(uint8Array); }
const secret = encode("文本内容"); console.log(`${secret}】`);
const plain = decode(secret); console.log(`${plain}】`);
|
完成
参考文献
张洪Heo的博客