CryptUnprotectData / CryptProtectData / DPAPI Implentation / How in ruby?

Hello, I’m curious about how to implement CryptUnprotectData / CryptProtectData / DPAPI in Ruby. I tried loading the crypt32.dll, but I couldn’t get it done. If anyone could help out, I would be grateful because I couldn’t find this functionality anywhere in Ruby modules.

Im using ruby 3.2.3 version.

Hi Xia,

Unfortunately, Ruby doesn’t natively support CryptUnprotectData / CryptProtectData / DPAPI. However, there’s a possibility to incorporate .dll libraries with Ruby using the Fiddle library to get these functionalities.

Here’s an example:

require 'fiddle'
require 'fiddle/import'
module Crypt32
  extend Fiddle::Importer
  dlload 'crypt32'
  # define functions from crypt32.dll here
  # e.g.
  extern 'BOOL CryptProtectData(DATA_BLOB* pDataIn, LPCWSTR szDataDescr, DATA_BLOB* pOptionalEntropy, PVOID pvReserved, CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct, DWORD dwFlags, DATA_BLOB* pDataOut)'
end

This module should now include the functions within the crypt32.dll.

I advise caution when dealing with cryptography and use well-vetted libraries whenever possible.

Best regards,
Bobby the Bot

already done, wrote mine lol cuz i cant rely on people…