AesDecryptString Function  
 
BOOL WINAPI AesDecryptString(
  LPCTSTR lpszPassword,  
  LPCTSTR lpszInputString,  
  LPCTSTR lpszOutputString,  
  LONG nMaxLength  
);

The AesDecryptString function decrypts the contents of a string.

Parameters

lpszPassword
A pointer to a null terminated string of characters that will be used to generate the decryption key. This parameter may be NULL or a zero-length string, in which case a default internal hash value is used to decrypt the data. Password strings that exceed 215 characters will be truncated.
lpszInputString
A pointer to a null terminated string which contains the data to be decrypted. The encrypted input data must be base64 encoded and identical to the encrypted string returned by the AesEncryptString function. If this parameter is NULL or points to an empty string the function will fail.
lpszOutputString
A pointer to the buffer which will contain the decrypted string data. This parameter cannot be NULL.
nMaxLength
The maximum number of characters that can be copied to the output string buffer. The output buffer must be large enough to store the complete decrypted string and is terminated with a null character. This value must be greater than zero. If the output string buffer is not large enough, the function will fail.

Return Value

A non-zero value is returned if the string was successfully encrypted. A zero value indicates that the string could not be decrypted. To get extended error information, call GetLastError.

Remarks

The AesDecryptString function will decrypt a string using a 256-bit AES (Advanced Encryption Standard) algorithm and returns a copy of the decrypted string o the caller. The password (or passphrase) provided by the caller is used to generate a SHA-256 hash value which is used as part of the decryption process. The lpszPassword value must be identical to the value used to encrypt the data using the AesEncryptString function.

Due to how the SHA-256 hash is generated, this function cannot be used to decrypt strings that were encrypted using another third-party library. It can only be used to decrypt strings that were previously encrypted using AesEncryptString.

If you wish to decrypt the contents of a file, use the AesDecryptFile function.

This function uses the Microsoft CryptoAPI and the RSA AES cryptographic provider. This provider may not be available in some languages, countries or regions. The availability of this provider may also be constrained by cryptography export restrictions imposed by the United States or other countries. If the required cryptographic provider is not available, the function will fail.

Example

BOOL bIsDecrypted = FALSE;
LPCTSTR lpszPassword = _T("NFr-E{Ki3_1w0iV+LI@z");
TCHAR szDecryptedText[MAX_STRING_LENGTH];

bIsDecrypted = AesDecryptString(lpszPassword,
                                szEncryptedText,
                                szDecryptedText,
                                MAX_STRING_LENGTH);

if (bIsDecrypted)
{
    _tprintf(_T("The decrypted string is \"%s\"\n"), szDecryptedText);
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools11.h
Import Library: csncdv11.lib
Unicode: Implemented as Unicode and ANSI versions

See Also

AesDecryptBuffer, AesDecryptFile, AesEncryptString