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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds