Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / VC++
Tip/Trick

Operation Password: CryptoAPI with AES 256

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Jun 2014CPOL2 min read 34.7K   2.6K   20   8
Using the Wincrypto in the Windows SDK

Introduction

I had to deal with some cryptography and found not a lot of clean and understandable stuff and so I want to share my results. The GUI is designed for demonstration use, and so the passwords are visible.

Image 1

Background

Security is a problem of today: criminals, identity thefts and observation is a common news today. Everybody knows the massive observations since the horrible news of honest Edward Snowden, which looks like the Watergate scandal of our times.

I guess that Microsoft complies to US laws and so the government will have some ways to crack these encryption.

Using the Code

The WinCrypto-API is used straightforward because I decided to use the most advanced Algorithm with AES 256. Who wants or needs another algorithm should learn from the MSDN. The best starting point is the startup function CryptAcquireContext.

Next comes the first pitfall. The CryptHashData is somehow tricky, because NOT EVERY algorithm is supported by every provider. And there may be also minor differences between some flavor of Windows (XP and 2003) or even Service packs.

So I have decided to encapsulate the API in a class which can be easily accessed from the outside by creating an object and providing a password. So in my class is some error handling and some cleanup code to close the used handles. To use the Crypto-API, you need the wincrypt-header and the library "advapi32.lib". I like the solution to include both in the implementation file, because then I am done for all configurations and I can also use the file in other projects.

C++
#include <wincrypt.h>
#pragma comment (lib, "advapi32")

The use of my class from outside is very straightforward.

C++
 CryptoApi ca;
 ca.Init(password);

 if( ca.EnCrypt( cryptedBuffer, dataLen, sizeof(cryptedBuffer) ) )
 {
   TRACE( "Encryption success");
   lenEncrypted = dataLen;//store for decryption !!! 
   ctlOutput.SetWindowText(TEXT("The secret is now encrypted"));
}

Take care of the length of the encrypted buffer: it is needed for decryption.

Here is the decryption, which I provided in an extra function and new object.

C++
 CryptoApi ca2;
 ca2.Init(decryptPassword);
 
 DWORD dataLen = lenEncrypted; //is needed for decryption

 if( ca2.DeCrypt( deCryptedBuffer, dataLen ) )
 {
   TRACE( "Decryption success");
   CString csDecrypted;
   memcpy( csDecrypted.GetBufferSetLength(dataLen/sizeof(TCHAR)), deCryptedBuffer, dataLen );//(one char is 2 bytes long)
   csDecrypted.ReleaseBuffer();
   ctlOutput.SetWindowText(TEXT("Decrypted secret is: ") + csDecrypted );
}

For the output, I copied the buffer to CString buffer for a useful output in my GUI.

Points of Interest

As expected, the WinCrypto provided some huzzle. But now in understanding is it a fine way to improve security.

The security stands and falls with the password, so the best way is an individual password. And "top of the pops" is to NOT store the password anywhere. So anybody has a chance to get or guess it.

Very Important hint: The password boxes should have the password style, so the value is hidden. If have chosen normal text style to demonstrate encryption and decryption.

In a public release, the resources should be fixed. In the resource editor, it should look like this:

Image 2

History

  • Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am living in germany and now living from programming for some Years. In my spare time I like sports as jogging, playing football (soccer) and basketball.

We must take care for our planet, because we and our family has no other. And everybody has to do something for it.

Comments and Discussions

 
QuestionThere is an error when encrypt Pin
kevin_fly4-Jul-16 0:27
kevin_fly4-Jul-16 0:27 
QuestionThis was a tip... Pin
OriginalGriff18-Jun-14 6:12
mveOriginalGriff18-Jun-14 6:12 
AnswerRe: This was a tip... Pin
KarstenK19-Jun-14 21:55
mveKarstenK19-Jun-14 21:55 
GeneralRe: This was a tip... Pin
OriginalGriff19-Jun-14 22:07
mveOriginalGriff19-Jun-14 22:07 
GeneralRe: This was a tip... Pin
KarstenK19-Jun-14 23:13
mveKarstenK19-Jun-14 23:13 
GeneralRe: This was a tip... Pin
OriginalGriff19-Jun-14 23:31
mveOriginalGriff19-Jun-14 23:31 
Very simple: you can want all you want.
I want to win the lottery.
I want to command the Enterprise.

I'm aware that neither of those are going to happen.

This isn't an article: it just doesn't qualify. It qualifies as a tip, which is what you submitted it as and it was moderated as. Trying to "game the system" by getting it passed as a tip and "upgrading" it to an article is not something we are very fond of - and class as abuse. People have been rightly banned for this behaviour before.

Sorry, but we have standards which must be met in order to qualify as an article; otherwise it degrade the perceived quality of all other articles, and of the site as a whole. This doesn't meet them, or even come close.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

GeneralRe: This was a tip... Pin
KarstenK20-Jun-14 22:05
mveKarstenK20-Jun-14 22:05 
GeneralRe: This was a tip... Pin
OriginalGriff20-Jun-14 22:17
mveOriginalGriff20-Jun-14 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.