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?


MSDN Data Encryption and Decryption Functions

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.

  _In_   HCRYPTPROV hProv,
  _In_   ALG_ID Algid,
  _In_   DWORD dwFlags,
  _Out_  HCRYPTKEY *phKey

Possible ALG_IDs

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 EVP_EncryptInit, EVP_EncryptUpdate, and EVP_EncryptFinal.

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.

  1. 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.
  2. 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.
  3. 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.

  • as per your suggestion i developed code. I used CALG_AES_128 as second argument to have AES-128 encryption. As a output of AES encryption, the ciphertext length will be multiple of 16. But here length of the cipher text, output of CryptEncrypt() function, is equal to the input data length. So think it's not performing AES-128 encryption. I'm able decrypt the cipher text but still confused about type of encryption being used here. – liveforFun Mar 27 '15 at 10:44
  • i developed code for AES encryption, and now imm getting error like: more data available in CryptEncrypt function call. Can you tell me what is it? – liveforFun Mar 30 '15 at 8:42
  • Updated my answer. – RoraΖ Mar 30 '15 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.