-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test.js
126 lines (106 loc) · 5.42 KB
/
test.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const { publicEncrypt, privateDecrypt, generateKeyPair, createCipheriv, createDecipheriv, constants } = require('crypto')
const generateRsaKey = () => new Promise((resolve, reject) => {
generateKeyPair(
'rsa',
{
modulusLength: 2048, // It holds a number. It is the key size in bits and is applicable for RSA, and DSA algorithm only.
publicKeyEncoding: {
type: 'spki', // Note the type is pkcs1 not spki
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8', // Note again the type is set to pkcs1
format: 'pem',
// cipher: "aes-256-cbc", //Optional
// passphrase: "", //Optional
},
},
(err, publicKey, privateKey) => {
if (err) {
reject(err)
return
}
resolve({
publicKey,
privateKey,
})
},
)
})
// generateRsaKey().then(({ publicKey, privateKey }) => {
// console.log(publicKey)
// console.log(privateKey)
// })
const rsaEncrypt = (buffer, key) => {
return publicEncrypt({ key, padding: constants.RSA_PKCS1_OAEP_PADDING }, buffer).toString('base64')
}
const rsaDecrypt = (buffer, key) => {
return privateDecrypt({ key, padding: constants.RSA_PKCS1_OAEP_PADDING }, buffer)
}
const publicKey = `
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0rggENU2JbXgYGoQBIyqlBJP76mgMKh8
5gRIUsAwoq/Oj7qEoKYX9jqtnRgAwEPIV7aLMQxGryfm9fGlohDcUPtcF6za5l6L9Szd+0McOCxZ
SY98/pPFdTYnBHRHPrHHYq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b/s2FTzmYHqcSQ66xsn4XzueaCrj9tqUuYIjYnnVM4lL
ux7CgGAwB6moBlUVcDbPZEMJEXXvDE6+oWg3eLGp1S5U7Q8ab0/9laC+c1ncMrgYhBNJ9RhvzimC
/+LPNbdmDQKBgQDleoBdsCDPIpbJVGjkgwN1T6aDPfs+A2lKx0z8Grzmv17HlhYqjy5Jvdx8bulR
0xY81YJ+h8dFD9e9fHdlxpfGj+gtHB8oGzjdN6JWfob6im+XAmg2ftWd1pHx9KBXkpL34NpqleJF
EH7LjGcMjbnceVAM6FEAiPI8BE6RrM91jwKBgQCMzIkzxa0oxKOWfYZVV1qVHS8jt2sZkwafOemt
JWqusMoQ7MubWXJXJdMywFq8752wFciK3pKxviNaumMq9kFoIN4dtfLnEc/mmlRgEORjeDRGvDSd
SAvqVpcrkpknlk64A32mYcHRq8uqB0FtiNuHo6RCChHm9d8bXdUmM5B0CQKBgQCM6xYq8j6jlHUO
O2SSdxXHk1sImyZO5Z9iCVNwOScpd/lHDRadmgFtzUa5rw5ebgbo4qBY/R5Ufa8ZMHbNrA+GItcL
5IoJgfYAeuqYvOg8sIhoLlU6qdaaL6q972ALhvnzeEQIUfR6Pu/uJVEet2WcS27qDju33WELlAV/
laRsZwKBgBzjfllho66+JLamWnthlDHqgEKZDiZF+1fZipOITd7jaA3W8teGc8v9YmSABG4b9IGd
M8XtAFeDYN/MHMKRq+nmLF6hvEQYHUgqYGZGkxX2HcKnRHUssbUOTWJynvRVNPL/k9g9q0d6PeSS
DaE3OO3AUN8voDBaHDII1YscbEBg
-----END PRIVATE KEY-----
`
// const encrypted = rsaEncrypt('123', publicKey)
// console.log(encrypted)
// // const encrypted = 'lJVUCFSV2K90ZdCeosUbtek/wZPmqmKR7ShsP2vfheldde6o9e2Qrmj1QojEwsZtjvq61FCmwpX46LkbsLY/jpM17PUZeqQHhqCy4Rz/hIyMCyIQTPwH5907pIwcpQH2XpJ45/hrjkhLhGU9pkZXtr3qkJiRTi0nllu7z6p6Qf0Hx/zYGxe41VVVnq/9t5xkoUyAfknEn1LMAJyJVft4pD43vTn4tz34+cf7GuzlC4xPiUyKC/trDGBW0kEQBPIaRpd7q1ab9x5fg8mffhBSDR3o+PvVuq3UP02MwpoMDs2bnnwzYawuGv87VNsvEHcvTkZDnh8ME9vtbQboLWVD5w=='
// console.log(rsaDecrypt(Buffer.from(encrypted, 'base64'), privateKey).toString())
const aesEncrypt = (buffer, mode, key, iv) => {
const cipher = createCipheriv(mode, key, iv)
return Buffer.concat([cipher.update(buffer), cipher.final()])
}
const aesDecrypt = function(cipherBuffer, mode, key, iv) {
let decipher = createDecipheriv(mode, key, iv)
return Buffer.concat([decipher.update(cipherBuffer), decipher.final()])
}
// const aesKey = Buffer.from('123456789abcdefg')
// const iv = Buffer.from('012345678901234a')
// // const encryptedAes = aesEncrypt(Buffer.from('hello'), 'aes-128-cbc', aesKey, iv)
// // console.log(encryptedAes)
// // // const encryptedAes = '4zbNfntuHPrHtPvhEVC10Q=='
// // console.log(aesDecrypt(Buffer.from(encryptedAes, 'base64'), 'aes-128-cbc', aesKey, iv).toString())
// // const encryptedAes = aesEncrypt(Buffer.from('hello'), 'aes-128-ecb', aesKey, '')
// // console.log(encryptedAes)
// const encryptedAes = 'oEShKajDOUILq3cVoRv0iw=='
// console.log(aesDecrypt(Buffer.from(encryptedAes, 'base64'), 'aes-128-ecb', aesKey, '').toString())
const text = '{"id":"3779629","n":100000,"p":1}'
const iv = Buffer.from('0102030405060708')
const presetKey = Buffer.from('0CoJUm6Qyw8W8jud')
const secretKey = [56, 70, 110, 77, 99, 98, 51, 117, 67, 98, 85, 73, 118, 80, 104, 70]
// Rn061YcbiMv3hQJlOLNklgQqbcUEF2YyiShXN8kevX3z+iU8j1qHhNEVEoNTNTPQ
// 4dbKbQjGbYdp/Q0bEwCTHcoB8vEQZNc5OUxz6VxScq4AuCYNHwWY44GJrfYuMV7GqQlC/88WdKg4w9ILJGAx5w==
const r1 = aesEncrypt(text, 'aes-128-cbc', presetKey, iv)
console.log(r1.toString('base64'))
console.log(aesEncrypt(r1, 'aes-128-cbc', Buffer.from(secretKey), iv).toString('base64'))