Мне нужно расшифровать dat файл таким образом:
.dat -> xor (ключ 11) -> base64 -> zlib -> plaintext
и по возможности перекодировать обратно.
Stack Overflow на русском — это сайт вопросов и ответов для программистов. Присоединяйтесь! Регистрация займёт не больше минуты.
Присоединиться к сообществуМне нужно расшифровать dat файл таким образом:
.dat -> xor (ключ 11) -> base64 -> zlib -> plaintext
и по возможности перекодировать обратно.
Теоретически порядок такой:
'use strict';
const fs = require('fs');
const zlib = require('zlib');
// Encoding.
{
const text = 'abcd\n';
console.log(JSON.stringify(text));
const notCompressedBuffer = Buffer.from(text);
console.log(notCompressedBuffer);
const compressed = zlib.gzipSync(notCompressedBuffer);
console.log(compressed);
const base64 = compressed.toString('base64');
console.log(base64);
const key = 11;
const xorred = Buffer.from(
Array.from(
Buffer.from(base64),
byte => byte ^ key
)
);
console.log(xorred);
fs.writeFileSync('test.dat', xorred, null);
}
// Decoding.
{
const xorred = fs.readFileSync('test.dat');
console.log(xorred);
const key = 11;
const base64 = Buffer.from(
Array.from(
xorred,
byte => byte ^ key
)
).toString();
console.log(base64);
const compressed = Buffer.from(base64, 'base64');
console.log(compressed);
const notCompressedBuffer = zlib.gunzipSync(compressed);
console.log(notCompressedBuffer);
const text = notCompressedBuffer.toString();
console.log(JSON.stringify(text));
}
Output:
"abcd\n"
<Buffer 61 62 63 64 0a>
<Buffer 1f 8b 08 00 00 00 00 00 00 0a 4b 4c 4a 4e e1 02 00 ac a4 8a 58 05 00 00 00>
H4sIAAAAAAAACktMSk7hAgCspIpYBQAAAA==
<Buffer 43 3f 78 42 4a 4a 4a 4a 4a 4a 4a 4a 48 60 7f 46 58 60 3c 63 4a 6c 48 78 7b 42 7b 52 49 5a 4a 4a 4a 4a 36 36>
<Buffer 43 3f 78 42 4a 4a 4a 4a 4a 4a 4a 4a 48 60 7f 46 58 60 3c 63 4a 6c 48 78 7b 42 7b 52 49 5a 4a 4a 4a 4a 36 36>
H4sIAAAAAAAACktMSk7hAgCspIpYBQAAAA==
<Buffer 1f 8b 08 00 00 00 00 00 00 0a 4b 4c 4a 4e e1 02 00 ac a4 8a 58 05 00 00 00>
<Buffer 61 62 63 64 0a>
"abcd\n"
Но это абстрактный шаблон, в вашем случае могут быть дополнительные условия.