I am writing a code to perform encryption and decryption using CryptEncrypt and CryptDecrypt functions, a part of Windows' Crypto API. Can anyone tell me what kind of encryption these two function use? Is it using AES or DES or 3DES or others?
You choose what algorithm to use.
You have to use
CryptAcquireContext to get a handle to the Crypto Service Provider. Then call
CryptGenKey to generate an encryption key object. This function is where you define the algorithm you want to use. You need to also call
CryptCreateHash to create a handle to a hash object. Then you call
CryptEncrypt with the key object and hash object you just generated.
BOOL WINAPI CryptGenKey( _In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Out_ HCRYPTKEY *phKey );
Most encryption implementations require you to call a final function to finish the encryption. This will take into account any padding that needs to be added, and probably some other algorithmic cleanup. Generally what this means is the last block of encrypted data is not output to the buffer until this final function is called. For example, in OpenSSL (when using the EVP functions) you have
Microsoft's implementation of
CryptEncrypt provides a
boolean final argument. When you're encrypting your last section of data you need to set this value to
TRUE. If you're only encrypting a single block size (or less) of data then you need to set this value to
TRUE. The remarks section of
CryptEncrypt explains what they perform when its set to true.
- If the key is a block cipher key, the data is padded to a multiple of the block size of the cipher. If the data length equals the block size of the cipher, one additional block of padding is appended to the data. To find the block size of a cipher, use CryptGetKeyParam to get the KP_BLOCKLEN value of the key.
- If the cipher is operating in a chaining mode, the next CryptEncrypt operation resets the cipher's feedback register to the KP_IV value of the key.
- If the cipher is a stream cipher, the next CryptEncrypt resets the cipher to its initial state.
CryptDecrypt provides the same
boolean final variable, and the same operation needs to take place when decrypting the data.