Hashing Streams in VC++ by shours

There are two categories of cryptographic algorithms: symmetric algorithms and asymmetric algorithms.
Symmetric cryptographic algorithms all use the same key to encrypt and decrypt. Some symmetric cryptographic algorithms are: DES, Triple DES, IDEA or AES.

The asymmetric algorithms have at least two keys, one public and one private. The sender encrypt with the public key and the receiver decrypt with the private key. The worst issue about asymmetric algorithms is that they are slower compared with the symmetric algorithms. The longer the key is, safer the encryption mechanism is.

A hash is taking a block of code and creating an output string of a block of code that represents a digest (the message digest). Hashing a stream means that, on one hand, you give the hashing algorithm some data (in a string format) and you will get, on the other hand, the digest. This is theoretically a one-way operation, meaning that one string generates one digest other string generates other digest. So if you change one bit to your input string you will get a totally different string. They say it is unlikely to get the same digest starting from different strings. Mathematically you will code the n bits on fewer bits so that’s way this is possible. Starting from the digest it is difficult to obtain the input-string.

Hashing algorithms:

– Secure Hash Algorithm (SHA-1)

– Message Digest (MD2, MD4, MD5)

Next you will see how to use a MD5 algorithm. This algorithm is public, so you will found it on the Internet, even the source code written in c++. We will concentrate in using the windows libraries. In your visual studio folder you will found in the Include directory one file named wincrypt.h. This is the most important file for building which is needed in building this example. Here, there are defined a large variety of functions and methods that allows you to write some cryptographic mechanisms. You will found here among others, the SHA and the MD2, MD4 and MD5 algorithms.

Let’s take a look at my example. I have created a Dialog based application with two edit boxes and a button. The user will insert some characters in the input edit box, will click then the digest box and will obtain the digest of his message generated by the MD5 algorithm.

When you will click the button Digest, the input string from the first edit box will be passed to the digest function that will generate the digest from the second edit box.

The following code will be called to digest the message:

HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
BYTE bHash[0x7f];
DWORD dwHashLen= 16;
DWORD cbContent= m_string_edit.GetLength();
BYTE* pbContent= (BYTE*)m_string_edit.GetBuffer(cbContent);

CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET);
CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash);
CryptHashData(hHash, pbContent, cbContent, 0);
CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0);

m_digest_edit.Empty();
CString tmp;
for (int i = 0; i<16; i++)
{
tmp.Format("%02x", bHash[i]);
m_digest_edit+=tmp;
}

CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
m_string_edit.ReleaseBuffer();

First you will create a context, second you will create the hash structure. You can choose from MD2, MD4, MD5 or SHA. Then you will crypt the message using CryptHashData. Next we will take the digest and presenting in a pleasant manner (hexa values). You will finish the hashing by destroying the hash structure and releasing the context.

By: Dragos BREZOI.