question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to use node-ffi with the crypt32.dll and CryptProtectData/CryptUnprotectData functions?

See original GitHub issue

i want to use the node-ffi libary with the windows crypt32.dll to use data protection api and the functions “CryptProtectData” and “CryptUnprotectData”: (https://msdn.microsoft.com/de-de/library/windows/desktop/aa380261(v=vs.85).aspx).

i tried the following:

var crypt32 = new FFI.Library('crypt32',
{
   'CryptProtectData': [
      'int32', [ 'string', 'string', 'string', 'string', 'string', 'string', 'string' ]
   ]
});

but i am not sure in which way to use the variable types. how can i use the c++ type “DATA_BLOB” or a pointer in nodejs?

can you post a working sample for that case? thank you.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

5reactions
magengbincommented, Aug 15, 2017

I try …

const fs = require('fs'); 
const ref = require('ref');
const ffi = require('ffi');
const Struct = require('ref-struct')
const Array = require('ref-array')
const DATA_BLOB = Struct({
    cbData: ref.types.uint32,
    pbData: 'string' // ref.refType(ref.types.byte)
})
const PDATA_BLOB = new ref.refType(DATA_BLOB)
const Crypto = new ffi.Library('Crypt32', {
    'CryptUnprotectData': ['bool', [PDATA_BLOB, 'string', 'string', 'void *', 'string', 'int', PDATA_BLOB]],
});
const data_blob_input = new DATA_BLOB();
data_blob_input.cbData = row.encrypted_value.length;
data_blob_input.pbData = row.encrypted_value;
const data_blob_output = new DATA_BLOB();
const result = Crypto.CryptUnprotectData(data_blob_input.ref(), null, null, null, null, 0, data_blob_output.ref());
cookies[row.name] = data_blob_output.pbData.slice(0, data_blob_output.cbData);
console.log(row.name, cookies[row.name])

It is working ok.

3reactions
Wackerbergcommented, Oct 21, 2017

Below is a complete example that worked for me.

const fs = require("fs");
const ref = require("ref");
const ffi = require("ffi");
const Struct = require("ref-struct");

const DATA_BLOB = Struct({
    cbData: ref.types.uint32,
    pbData: ref.refType(ref.types.byte)
});
const PDATA_BLOB = new ref.refType(DATA_BLOB);
const Crypto = new ffi.Library('Crypt32', {
    "CryptUnprotectData": ['bool', [PDATA_BLOB, 'string', 'string', 'void *', 'string', 'int', PDATA_BLOB]],
    "CryptProtectData" : ['bool', [PDATA_BLOB, 'string', 'string', 'void *', 'string', 'int', PDATA_BLOB]]
});

function encrypt(plaintext) {
    let buf = Buffer.from(plaintext, 'utf16le');
    let dataBlobInput = new DATA_BLOB();
    dataBlobInput.pbData = buf;
    dataBlobInput.cbData = buf.length;
    let dataBlobOutput = ref.alloc(DATA_BLOB);
    let result = Crypto.CryptProtectData(dataBlobInput.ref(), null, null, null, null, 0, dataBlobOutput);    
    let outputDeref = dataBlobOutput.deref();
    let ciphertext = ref.reinterpret(outputDeref.pbData, outputDeref.cbData, 0);
    return ciphertext.toString('base64');
};

function decrypt(ciphertext) {
    let buf = Buffer.from(ciphertext, 'base64');
    let dataBlobInput = new DATA_BLOB();
    dataBlobInput.pbData = buf;
    dataBlobInput.cbData = buf.length;
    let dataBlobOutput = ref.alloc(DATA_BLOB);
    let result = Crypto.CryptUnprotectData(dataBlobInput.ref(), null, null, null, null, 0, dataBlobOutput);
    let outputDeref = dataBlobOutput.deref();
    let plaintext = ref.reinterpret(outputDeref.pbData, outputDeref.cbData, 0);
    return plaintext.toString('utf16le');
};

let text = "åäöÅÄÖ_éÉóÓ D25P";
let ciphertext = encrypt(text);
let plaintext = decrypt(ciphertext);

console.log("text:", text);
console.log("ciphertext:", ciphertext);
console.log("plaintext:", plaintext);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Crypt32.dll Versions - Win32 apps - Microsoft Learn
dll is the module that implements many of the Certificate and Cryptographic Messaging functions in the CryptoAPI, such as CryptSignMessage.
Read more >
CryptProtectData: C++ Code for calling function from Crypt32 ...
I am using "MAPI Download configuration guidance.docx" (can be downloaded from www .microsoft.com/en-us/download/details.aspx?id=39045 the piece of code ...
Read more >
How to Fix Crypt32.dll Not Found or Missing Errors - Lifewire
A troubleshooting guide for crypt32.dll is missing and similar errors. ... If System Restore doesn't fix the DLL error, run System File ...
Read more >
crypt32.dll Missing Error on Windows | 2020 | Fix #2 - YouTube
crypt32. dll Missing Error on Windows | 2020 | Fix #2. 2.8K views · 2 years ago ...more. STS Tutorial. 7.95K. Subscribe. 31....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found