How to Encrypt/Decrypt a String

Screenshot of example program

Environment: VC6 SP5, Windows XP SP1p

Based on the article "Encrypting and Decrypting Sensitive Data Using CryptoAPI" by Jorge Lodos in the Miscellaneous/CryptoAPI section and many questions in the forum (how to encrypt/decrypt a string—also from me), I have adapted this project for this purpose. My project doesn't use the Registry; the string is returned from the function.

Three functions are needed for encryption/decryption:

  • Initialize CryptoAPI.
  • Encryption function that takes the original string and the key.
  • Decryption function that takes the encrypted string and the key and returns the decrypted function.

The prototypes for the functions are:

BOOL SetupCryptoClient();
BOOL EncryptString(TCHAR* szPassword,TCHAR* szEncryptPwd,TCHAR
                          *szKey);
BOOL DecryptString(TCHAR* szEncryptPwd,TCHAR* szPassword,TCHAR
                          *szKey);

The meanings of the functions and parameters are following:

  • BOOL SetupCryptoClient();
    Initialize the crypto client
  • BOOL EncryptString(TCHAR* szPassword,TCHAR*
                              szEncryptPwd,TCHAR *szKey)
    
    Encrypt a string:
    • Where szPassword is the original password,
    • szEncryptPwd is the result, and
    • szKey is the key for encryption.
  • BOOL DecryptString(TCHAR* szEncryptPwd,TCHAR*
                              szPassword,TCHAR *szKey)
    
    Decrypt a string:
    • Where szEncryptPwd is the encrypted password,
    • szPassword is the decrypted password, and
    • szKey is the key for decryption, which must be the same as in the encryption function.

The main routine of the console application should explain the function of the encryption/decryption functions.

Condition for Compiling

It costs me hours to resolve a compiler error where the data type HCRYPTPROV is not defined. The reason:

  • clear the #define WIN32_LEAN_AND_MEAN (doesn't compile often-used parts)
  • #define _WIN32_WINNT 0x0400
  • For Linker: add library "advapi32.lib" in settings

Considerations for the C++ Class

  • Generate a class: for example, CCryptString.
  • In the constructor, call the init-function SetupCryptoClient and save the result in a member variable.
  • Copy the encryption/decryption functions in the class.

The second program contains an example of such a class and also the usage.

For background information about decryption, read the above-mentioned article and the MSDN. A lot of stuff also be found on the Internet.

I hope this project meets your expectations.

If you found any errors, please let me know. If I have time, I will implement solutions to the errors found.

Best regards,
Ing. Georg Hasenöhrl
Working as a software developer (C++/VB/SQL) at Hakom (IT Consulting)

Downloads

Crypt String with C-functions - 5 Kb
Crypt String as C class - 6 Kb


Comments

  • Cheap Oakley Radarlock Path wholesale store

    Posted by zcsqkwttk on 06/27/2013 05:29pm

    Oakley Mens Sunglasses ,Oakley fashion sport sunglasses. Led the international status like a sports brand, in addition to a beautiful fashion appearance of sunglasses. Oakley sunglasses can be bought to enjoy their women, the eye of evil weather, We need in excess of the main anti-sun measures to counteract serious sun. Ray Ban Outlet ,Oakley, Sunglasses will you expect on the world leader in performance and innovative technology, unmatched processing on the smallest detail, there isn't a trouble, just change a new lens group. Oakley and enterprises to establish regular success, due to their amazing efforts. Virtually every generation, which Oakley exposed, exposed improvement. Fake Oakley Jawbone ,Oakley sunglasses, the first scope with the Hollywood stars, used often by the scope of such people to maintain her or his observation, prohibit anyone to view one more specific false Ray. Sunglasses maintenance and the maintenance of ordinary glasses is similar, general, regular cleaning, proper storage should create a good habit. Indicates that a specific glasses, which assists to ensure it truly is together with his or her actual eye quality, various factors unique importance. Oakley sunglasses are the ones who want to protect your professional riders of the sun through the ultimate visual clarity, specifically Kun pounds copy glare, polarized disposable lenses in the glasses Oakley You select an available selection of sunglasses, these sunglasses would be the activities arranged by polarization, the prevailing style, special and restricted model. They are extremely popular and unique design, comfort and consolation, you could be considering Oakley, sunglasses for eye care role and classy individually decorated customer favorite. Oakley can be a sports brand in the states, the leading portions of different kinds of functions, as well as casual wear, skiing, swimming, cycling and athletics sportswear. "Wearable electronics" to gather weapons Bluetooth solution, there isn't a cell cellphone Mp3 music player collective access and listen to music, from the framework of sunglasses produced extremely plans. Sun block is generally determined to not just be aware that you could have already installed a stylish plus much more attractive appearance, that is actually a great way to protect you resist the cruel sunlight. Oakley introduced several sunglasses, women's Oakley sunglasses not just utilize latest SWITCHLOCK manufacture, the lens might be changed any technology, and also with black and yellow color, you possibly can refer.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming webcast to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds