【代码】JS通过Unicode变体选择符实现隐写术

前言

变体选择符是一个基本多文种平面的 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
// Unicode变异选择符固定码点
const VS16_START = 65024; // U+FE00
const VS_EXT_START = 917760; // U+E0100

/**
* 编码
* @param {string} plainText - 待加密的普通明文字符串
* @returns {string} 隐形密文字符串(无可见字符)
* @throws {TypeError} 入参非字符串时抛出异常
*/
function encode(plainText) {
// 基础类型校验
if (typeof plainText !== "string") {
throw new TypeError("编码入参必须为字符串类型");
}

// 将明文编码为UTF-8字节数组
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;
}

/**
* 解码
* @param {string} secretText - 编码后的隐形密文字符串
* @returns {string} 解码后的原始明文字符串
* @throws {TypeError} 入参非字符串时抛出异常
*/
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);
}
}

// 字节数组转UTF-8字符串
const uint8Array = new Uint8Array(byteBuffer);
return decoder.decode(uint8Array);
}

// 加密
const secret = encode("文本内容");
console.log(`${secret}】`);

// 解密
const plain = decode(secret);
console.log(`${plain}】`);

完成

参考文献

张洪Heo的博客