Easiest way to decrypt PGP-encrypted files from VBA (MS Access)

15,025

Solution 1

A command line solution is good. If your database is an internal application, not to be redistributed, I can recommend Gnu Privacy Guard. This command-line based tool will allow you to do anything that you need to with regard to the OpenPGP standard.

Within Access, you can use the Shell() command in a Macro like this:

Public Sub DecryptFile(ByVal FileName As String)
  Dim strCommand As String
  strCommand = "C:\Program Files\GNU\GnuPG\gpg.exe " _
  & "--batch --passphrase ""My PassPhrase that I used""" & FileName
  Shell strCommand, vbNormalFocus
End Sub

This will run the command-line tool to decrypt the file. This syntax uses a plaintext version of your secret passphrase. This is not the most secure solution, but is acceptable if your database is internal and only used by trusted personnel. GnuPG supports other techniques to secure the passphrase.

Solution 2

You can use OpenPGPBlackbox (ActiveX edition) for this

Solution 3

Stu... I once had to write a "Secure SMTP" server in Java... The easiest, and quickest way to do this is to download and/or purchase PGP. They have an SDK that you can use to access in anything you want.

I'd have to go back and see if I had to write a COM wrapper, or if they already had one. (I wrote this SMTP server about 10 years ago). Anyways, don't get discouraged. About 5 years ago, I wrote an entire PGP based application (based on the openPGP RFC) in C++, but the catch was, I was NOT allowed to use any existing libraries. So I had to write all that stuff myself. And, I used GPG, OpenPGP, and PGP for testing, etc....

So, I could even provide help for you on how to decode this stuff in VBA. It's not impossible, (it may be slow as hell, but not impossible), and I'm NOT one to "shell out and run cmdline stuff to do work like this for you, as it will open you up to some SERIOUS security risks, as hurcane's suggestion (for example) will cause your passphrase to be displayed to tools like ProcExp). The first step is learning how PKE works, etc. Then, the steps you need to do to get what you want.

This is something I'd be interested in helping with since I'm always one to write code that everyone says can't be done. :) Plus, I own the source code of the app I wrote, because of of mergers, closures, etc...

It was originally written for the Oil and Gas industry, so I know it's secure. That's not to say I don't have ANY security flaws in the code, but I think it's stable. I know I have an issue with my Chinese Remainder Threory code.. For some reason when I use that short-cut, I can't decode the data correctly, but if I use the RSA "long way" it works...

Now, this application was never fully finished, so I don't support things like DSA Key-pairs, but I do support RSA key pairs, with SHA1, MD5, using IDEA, AES, (I THINK my 3DES code does not work correctly, but I may have fixed that since). I didn't implement compression yet, etc... But, I'd love a reason to go back and work on this code again.

I /COULD/ make you a COM object that you could call from VBA passing the original Base64 data in, along with the Base64 key data, (or a pointer to a key file on disk), and a passpsshrase to decode files....

Think about it... Let me know..

Over the years, I have collected vbScript code for doing things like MD5, SHA1, IDEA, and other crypto routines, but I didn't write them. Hell, you could probably just interface with Microsoft's CryptoAPI, and break each action down to it's core parts and still get it to work. (You will not find a Micosoft CryptoAPI call like "DecryptPGP()"... It'd all have to be done in chunks).

Lemme know if I can help.

Solution 4

PGP has a commandline option for decrypting files.

We have a batchfile that does the decryption, passing in the filename to be decrypted:

Batch file:

"C:\Program Files\Network Associates\PGPNT\pgp" +FORCE %1 -z *password* 

We than call that from a VBS:

  Command = "decrypt.bat """ & FolderName & FileName & """"

  'Executes the command script.
  Set objShell = WScript.CreateObject ("WSCript.shell")
  Command = "cmd /c " & Command
  objShell.run Command, 1, True

Hope that points you in a useful direction.

Solution 5

I would look for a command line encrypter / decrypter and just call the exe from within your Access application, with the right parameters.

There is no PGP encrypter / decrypter in VBA that I know of.

Share:
15,025
stucampbell
Author by

stucampbell

Updated on June 25, 2022

Comments

  • stucampbell
    stucampbell almost 2 years

    I need to write code that picks up PGP-encrypted files from an FTP location and processes them. The files will be encrypted with my public key (not that I have one yet). Obviously, I need a PGP library that I can use from within Microsoft Access. Can you recommend one that is easy to use?

    I'm looking for something that doesn't require a huge amount of PKI knowledge. Ideally, something that will easily generate the one-off private/public key pair, and then have a simple routine for decryption.

    • LarryF
      LarryF over 15 years
      Generating a KeyPair is about 90% of the difficulty in PKE... You are going to have to learn PKI, PKE, etc in order to pull this off. But you do not have to become an expert in it, just a good understanding how it all works...
  • David-W-Fenton
    David-W-Fenton over 15 years
    This has the problem of needing to call SHELL in Access, which will run asynchronously. In that case, you could use the ShellAndWait code from the Access Web, mvps.org/access/api/api0004.htm .
  • David-W-Fenton
    David-W-Fenton over 15 years
    To run this synchronously instead of asynchronously (as the code above will execute), try ShellAndWait: mvps.org/access/api/api0004.htm .