Windows C/C++ Crypto API Examples and tips
Asked Answered
L

4

28

I'm asking this question because I've spent the best part of a day trawling through msdn docs and other opaque sources for simple straightforward guidelines on how to get started with the Windows C/C++ Crypto API.

What I'd like to see is some example code, typical include paths, linking guidelines, etc, anything useful really. I know this is an imprecise question but I reckon imprecise answers are better none at all.

I'll get the ball rolling with my own meager findings...

Leukorrhea answered 25/1, 2011 at 17:7 Comment(3)
Do you mean classic CryptoAPI as in Win2k and XP, or CNG ("CryptoAPI Next Generation") as in Vista and Win7? The two are not the same.Hemicellulose
Hmm, I suppose I mean the CryptoAPI but I'm compiling on a Windows 7 system.Leukorrhea
It would be better to use CNGJared
L
28

Here's a bunch of examples I've found....

MSDN has these examples scattered around the docs

This website provides a good overview of the concepts along with cross-platform examples

Leukorrhea answered 26/1, 2011 at 17:34 Comment(2)
You can just merge your two answers.Freeway
Wow, and not an example that simply encrypts data.Scorch
L
18

The msdn docs are here: http://msdn.microsoft.com/en-us/library/aa380252.aspx

This is the main include file: #include <wincrypt.h>

The cryptography bits are included as part of the Windows SDK, which is typically installed in %PROGRAMFILES(x86)%\Microsoft SDKs\Windows\SDKVERSION (e.g., C:\Program Files\Microsoft SDKs\Windows\v6.0A). The headers are typically in %WINDOWSSDK%\Include, and the related libraries are in %WINDOWSSDK%\Lib.

You must link to the cryptography libraries explicitly. Assuming you're in Visual Studio, you can add the reference by right clicking on the C++ project, choosing properties, and selecting Configuration Properties -> Linker on the treeview at left. You can then specify crypt32.lib in the input field on the right.

Alternately, (assuming you're using msvc++) add

#pragma comment(lib, "crypt32.lib")

to your source.

Leukorrhea answered 25/1, 2011 at 17:11 Comment(4)
I significantly revised and expanded this answer. Please check to ensure you think the changes are useful :)Anaemic
Ah!, thanks, I would never have thought of using the #pragma directive.Leukorrhea
I was using #include <wincrypt.h> and #include <cryptdlg.h> in my VC++2017 project and I got error "LNK2019" when callling their api functions. Addding the line #pragma comment(lib, "crypt32.lib") solved it! Thanks :DBrooklet
Wow, thanks, I wonder why this is not part of any MSDN guide.Elmore
U
6

There is also a lengthy example "Encryption using the Win32 Crypto API" over at the Code Project.

Unmeaning answered 2/6, 2012 at 10:20 Comment(0)
B
0

After searching for several hours, I found this one here: https://learn.microsoft.com/en-us/troubleshoot/windows/win32/get-information-authenticode-signed-executables

It's detailed, and it works.

Booher answered 30/3, 2023 at 14:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.