📜 node 中加解密
ECB(电子密码本模式:Electronic codebook)是最简单的块密码加密模式,加密前根据加密块大小(如 AES 为 128 位)分成若干块,之后将每块使用相同的密钥单独加密,解密同理。ECB 模式由于每块数据的加密是独立的因此加密和解密都可以并行计算,ECB 模式最大的缺点是相同的明文块会被加密成相同的密文块,这种方法在某些环境下不能提供严格的数据保密性。
CBC 模式对于每个待加密的密码块在加密前会先与前一个密码块的密文异或然后再用加密器加密。第一个明文块与一个叫初始化向量的数据块异或。CBC 模式相比 ECB 有更高的保密性,但由于对每个数据块的加密依赖与前一个数据块的加密所以加密无法并行。与 ECB 一样在加密前需要对数据进行填充,不是很适合对流数据进行加密。
与 ECB 和 CBC 模式只能够加密块数据不同,CFB(密文反馈:Cipher feedback)能够将块密文(Block Cipher)转换为流密文(Stream Cipher)。
OFB(输出反馈:Output feedback)是先用块加密器生成密钥流(Keystream),然后再将密钥流与明文流异或得到密文流,解密是先用块加密器生成密钥流,再将密钥流与密文流异或得到明文,由于异或操作的对称性所以加密和解密的流程是完全一样的。
查看支持那些加密
const crypto = require("crypto");
crypto.getCiphers();
1
2
2
vi
Crypto.randomBytes(16);
1
DES/ECB/pack5 demo
平安知鸟 用
// https://juejin.cn/post/6844903854148943886
// https://github.com/linxiaowu66/awesome-crypto/blob/master/lib/cipher.ts
const crypto = require("crypto");
/**
* 平安知鸟 用户同步 用的 DES/ECB/pacs5padding 加密
* @param text 需要加密的数据
* @param secret 加密密钥,管理平台查询
* @returns
*/
export function des_ecb(text: string, secret: string) {
console.log("DES/ECB 加密前:", text);
const keyBuffer = Buffer.alloc(secret.length, secret, "utf8");
// vi 初始化向量(IV),ecb 不需要
const cipher = crypto.createCipheriv("des-ecb", keyBuffer.slice(0, 8), "");
/**
* update方法
* 第一个参数:加密的数据
* 第二个参数:传入数据的格式,可以是'utf8', 'ascii', 'latin1'
* 第三个参数:加密数据的输出格式,可以是'latin1', 'base64' 或者 'hex'。没有执行则返回Buffer
*/
let encrypted = cipher.update(text, "utf8", "base64");
// console.log('DES/ECB encrypted:', encrypted);
const end = cipher.final("base64");
// console.log('DES/ECB final:', end);
console.log("DES/ECB 加密后:", encrypted + end);
return encrypted + end;
}
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
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
中信 党费通
const crypto = require("crypto");
class DesUtils {
/**
* 加密
* @param {*} text 需要加密的内容
* @param {*} secret 密钥
* @returns
*/
static encrypt = (text, secret) => {
const keyBuffer = Buffer.alloc(secret.length, secret, "utf8");
// vi 初始化向量(IV),ecb 不需要
const cipher = crypto.createCipheriv("des-ecb", keyBuffer.slice(0, 8), "");
/**
* update方法
* 第一个参数:加密的数据
* 第二个参数:传入数据的格式,可以是'utf8', 'ascii', 'latin1'
* 第三个参数:加密数据的输出格式,可以是'latin1', 'base64' 或者 'hex'。没有执行则返回Buffer
*/
const encrypted = cipher.update(text, "utf8", "hex");
// console.log('DES/ECB encrypted:', encrypted);
const end = cipher.final("hex");
// console.log('text:', text);
// console.log('secret:', secret);
// console.log("encrypted:", encrypted );
// console.log("end:", end );
return encrypted + end;
};
/**
* 解密
* @param {string} text 解密 密文
* @param {string} secret 密钥
* @returns
*/
static decrypy = (text, secret) => {
const keyBuffer = Buffer.alloc(secret.length, secret, "utf8");
const decipher = crypto.createDecipheriv(
"des-ecb",
keyBuffer.slice(0, 8),
""
);
const decrpyted = Buffer.concat([
decipher.update(Buffer.from(text, "hex")), // !!! hex !!!
decipher.final(),
]);
return decrpyted.toString();
};
}
const aaa = DesUtils.encrypt("Mima001", "3862616E6B323937");
console.log("加密 > ", aaa);
console.log("解密 > ", DesUtils.decrypy(aaa, "3862616E6B323937"));
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
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