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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import crypto from 'crypto';

const algorithm = 'aes-256-cbc'; // 使用的加密算法
const key = crypto.randomBytes(32); // 256 位密钥
const iv = crypto.randomBytes(16); // 初始化向量 (IV)

// 位运算加密函数
function bitwiseEncrypt(data) {
let encrypted = '';
for (let i = 0; i < data.length; i++) {
// 对每个字符进行位运算(这里使用 XOR 运算)
encrypted += String.fromCharCode(data.charCodeAt(i) ^ 0xAA); // 0xAA 是密钥
}
return encrypted;
}

// 位运算解密函数
function bitwiseDecrypt(data) {
let decrypted = '';
for (let i = 0; i < data.length; i++) {
// 对每个字符进行位运算(XOR 运算,密钥与加密时相同)
decrypted += String.fromCharCode(data.charCodeAt(i) ^ 0xAA); // 0xAA 是密钥
}
return decrypted;
}

// 加密函数
function encrypt(data) {
const dataString = JSON.stringify(data);
// 位运算加密
const bitwiseEncryptedData = bitwiseEncrypt(dataString);
const cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv);
let encrypted = cipher.update(bitwiseEncryptedData);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}

// 解密函数
function decrypt(encryptedString) {
try {
const [ivText, encryptedText] = encryptedString.split(':');
const iv = Buffer.from(ivText, 'hex');
const encrypted = Buffer.from(encryptedText, 'hex');
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), iv);
let decrypted = decipher.update(encrypted);
decrypted = Buffer.concat([decrypted, decipher.final()]);
const decryptedData = decrypted.toString();
// 位运算解密
const bitwiseDecryptedData = bitwiseDecrypt(decryptedData);
return JSON.parse(bitwiseDecryptedData);
} catch (e) {
return encryptedString;
}
}

// 加密或解密函数
function encryptOrDecrypt(data) {
if (typeof data === 'string') {
try {
const decrypted = decrypt(data);
if (decrypted !== data) {
return decrypted;
}
} catch (e) {
// 解密失败,可能是未加密的字符串
}
}
return encrypt(data);
}

// 示例用法
const originalData = { name: 'Alice', age: 25 };
// console.log('原始数据:', originalData);

// 加密
const encrypted = encryptOrDecrypt(originalData);
console.log('加密后的字符串:', encrypted);

// 解密
const decrypted = encryptOrDecrypt(encrypted);
console.log('解密后的数据:', decrypted);

// 测试非加密字符串
const plainText = 'This is a plain text';
const encryptedPlainText = encryptOrDecrypt(plainText);
console.log('加密后的普通文本:', encryptedPlainText);

const decryptedPlainText = encryptOrDecrypt(encryptedPlainText);
console.log('解密后的普通文本:', decryptedPlainText);

输出结果:

1
2
3
4
加密后的字符串: 7b8d204f46926e1b49ccfeb1af0b3dbe:d2956d326c3d426dbeca2964210b5c581000eef40e47a66643dbc4c6d6cefa57b4c23872e2b9789c6870d3846b74a05cc5762c96ad807f0e0541a8112fa9cb99
解密后的数据: { name: 'Alice', age: 25 }
加密后的普通文本: 7b8d204f46926e1b49ccfeb1af0b3dbe:6c7f5d551047c7e59eb3de379f2b94beb0c616cd5c2d87817f10d6e859ea49c97023f0df2bfeb6eb64d7aaab0b72140d
解密后的普通文本: This is a plain text