0

I see ton's of examples of this for C++ and C# but no luck with C. I'm trying to do a simple encryption of a string without referencing external DLLs. I started out trying cryptlib (just basic encryption before moving on to PGP) and with the following .c file:

#include "include/cryptlib/cryptlib.h"

void PGPEncrypt(char* input, char* output) {
    CRYPT_ENVELOPE cryptEnvelope;
    int bytesCopied;

    cryptCreateEnvelope(&cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_CRYPTLIB);

    // Below line makes things more efficient if you know the data size
    cryptSetAttribute(cryptEnvelope, CRYPT_ENVINFO_DATASIZE, strlen(input));

    // Push unencrypted, pop encrypted. 4000 is the size of output
    cryptPushData(cryptEnvelope, input, strlen(input), &bytesCopied);
    cryptFlushData(cryptEnvelope);
    cryptPopData(cryptEnvelope, output, 4000, &bytesCopied);

    cryptDestroyEnvelope(cryptEnvelope);
}

However, while compiling this worked, I realized when running the application that the cl32.lib was just a header library and required the cl32.dll file to be installed on the system. I have yet to figure out how to create a static cl32.lib file that I can include in my project that would not require the DLL. That would work, or another non-cryptlib solution.

I've also taken a look at Crypto++, but unfortunately the downloadable chm user guide is broken, and the manual is too detailed for me to follow without a guide of some kind.

I should throw out there that while I'm fairly familiar with C as a whole, most of my experience is old and in Linux, not using Visual Studio 2010 which I am doing now.

Thanks, Ben

Fmstrat
  • 1,492
  • 2
  • 17
  • 24

1 Answers1

1

You can include your external dependencies by compiling your program static, if you just want to remove the dependencies.

If you worry about missing DLLs on windows systems you can use the windows crypto API, which is present on all windows systems.

Cryptography Reference

Example C Program: Using CryptAcquireContext

If you don't want any of this you can still implement your own crypto library. But I don't recommend that, as it's full of potential pitfalls, even if you're a crypto expert.

Peter F.
  • 111
  • 1
  • 7
  • Thanks! This is likely my junior status among Visual Studio, but how would I compile a static EXE? I've searched for many a tutorial, but everything I find requires the cl32.lib file (import library) be included in the project, then the cl32.dll be in the same folder. Is there a specific way to actually embed the cl32.dll file within my EXE? If I can't get this to work, I'll check out the windows crypto, too. Thanks again. – Fmstrat Sep 23 '12 at 13:05
  • Also, took a look at the windows crypto API, and I think it may work for me with less file size, but can it handle pre-existing PGP public and private keys? I see tons of source examples where it creates a key, then uses that, but can't find anything about if I have an existing ASCII key file. – Fmstrat Sep 23 '12 at 13:29
  • As a note, looks like the answer is no for my second question: http://social.msdn.microsoft.com/Forums/en-US/visualfoxprogeneral/thread/c9155c02-f792-4ce0-b601-2c0e76251fec Thanks. Haven't figured out the first one (DLL) yet, though. – Fmstrat Sep 23 '12 at 13:35