Browse Source

Added Decrypt Buffer

master
xefglm 3 years ago
parent
commit
7045bb6e01
4 changed files with 51 additions and 1 deletions
  1. +3
    -1
      .gitignore
  2. +4
    -0
      decryptor.js
  3. +17
    -0
      native/decryptor.cc
  4. +27
    -0
      native/decryptor.h

+ 3
- 1
.gitignore View File

@ -1,3 +1,5 @@
node_modules/
package-lock.json
build/
build/
test.js
.vscode

+ 4
- 0
decryptor.js View File

@ -7,4 +7,8 @@ exports.getKey = (trackId) => {
exports.decryptFile = (key, inputFilename, outputFilename) => {
decryptor.decryptFile(key, inputFilename, outputFilename);
return;
}
exports.decryptBuffer = (key, data) => {
return decryptor.decryptBuffer(key, data);
}

+ 17
- 0
native/decryptor.cc View File

@ -26,9 +26,26 @@ void napiDecryptFile(const Napi::CallbackInfo& info) {
return;
}
Napi::Value napiDecryptBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string decKeyArg = info[0].As<Napi::String>().Utf8Value();
char* decKey = new char[17];
strcpy(decKey, decKeyArg.c_str());
size_t arrlen = info[1].As<Napi::Buffer<unsigned char>>().Length();
unsigned char* buffer = info[1].As<Napi::Buffer<unsigned char>>().Data();
unsigned char* decrypted = new unsigned char[arrlen];
decryptBytes(decKey, arrlen, buffer, decrypted);
return Napi::Buffer<unsigned char>::New(env, decrypted, arrlen);
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "getKey"), Napi::Function::New(env, napiGetKey));
exports.Set(Napi::String::New(env, "decryptFile"), Napi::Function::New(env, napiDecryptFile));
exports.Set(Napi::String::New(env, "decryptBuffer"), Napi::Function::New(env, napiDecryptBuffer));
return exports;
}

+ 27
- 0
native/decryptor.h View File

@ -43,4 +43,31 @@ void decryptFile(char* decKey, const char* inputfn, const char* outputfn) {
}
i++;
}
fclose(ofile);
fclose(ifile);
}
void decryptBytes(char* decKey, long length, unsigned char* data, unsigned char* output) {
unsigned char* bfKey = reinterpret_cast<unsigned char*>(decKey);
BF_KEY key;
BF_set_key(&key, 16, bfKey);
int count = length/2048;
int i=0;
for(i=0; i<count; i++) {
if (i % 3 == 0) {
unsigned char buffer[2048];
memcpy(buffer, &data[i*2048], 2048);
unsigned char decrypted[2048];
unsigned char IV[8] = {0,1,2,3,4,5,6,7};
BF_cbc_encrypt(buffer, decrypted, 2048, &key, IV, BF_DECRYPT);
memcpy(&output[i*2048], decrypted, 2048);
} else {
memcpy(&output[i*2048], &data[i*2048], 2048);
}
}
if (length % 2048 != 0){
memcpy(&output[i*2048], &data[i*2048], (length-(i*2048)));
}
}

Loading…
Cancel
Save