Help me to figure out this DES decryption....
I could get back the plaintext after decrypt.
Could anyone help me some ideas or any direction to solve this problem?
Thanks a lot,
T.H
#include <windows.h>
#include <wincrypt.h>
#include <iostream.h>
#include <stdio.h>
void HandleError(char *s);
void ByteToStr(DWORD cb, void* pv, LPTSTR sz);
void StrToByte( DWORD _dwLen, LPTSTR _pStr, LPBYTE _pByte);
#define KEY_LENGTH 0x00400000
int main(int argc, char* argv[])
{
HCRYPTPROV hCryptProv;
HCRYPTKEY hDESKey;
HCRYPTHASH hHashMD5;
BYTE pbData[56] = {0};
TCHAR szHexKey[113] = "DE867B34E5CC422F24F595BF2AC530D7EF24FCD983E67E7C93226F35BACAEA4536A5C534B63FB1FFC328AD55AB 7A1BD58B586488E6C62963";
TCHAR szData[17] = "TestData";
DWORD dwDataLen;
// Convert Hex Key to bytes.
StrToByte(lstrlen(szHexKey), szHexKey, pbData);
LPBYTE pbBLOB_Export = NULL;
// create a new keyset
if (!CryptAcquireContext(&hCryptProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
HandleError("Error during CryptAcquireContext!\n");
return 0;
}//end if.
////////////////////////////////////////////////////////////////////////////////////////////
// Create a hash object.
if(! CryptCreateHash(hCryptProv, // [in] HCRYPTPROV hProv
CALG_MD5, // [in] ALG_ID Algid
0, // [in] HCRYPTKEY hKey
0, // [in] DWORD dwFlags
&hHashMD5)) // [out] HCRYPTHASH *phHash
{
HandleError("Error during CryptCreateHash!\n");
return FALSE;
}//endif
// Call CryptHashData.
if(! CryptHashData( hHashMD5, // [in] HCRYPTHASH hHash
pbData, // [in] BYTE* pbData
sizeof(pbData), // [in] DWORD dwDataLen
0)) // [in] DWORD dwFlags
{
HandleError("Error during CryptHashData! Could not add data to the
Hash Object!");
return false;
}//endif
if(!CryptDeriveKey( hCryptProv,
CALG_DES,
hHashMD5,
KEY_LENGTH,
&hDESKey))
{
HandleError("Error during CryptGenKey!\n");
return 0;
}
dwDataLen = 8;
if(!CryptEncrypt( hDESKey, // [in] HCRYPTKEY hKey
0, // [in] HCRYPTHASH hHash
FALSE, // [in] BOOL Final
0, // [in] DWORD dwFlags
(LPBYTE)szData, // [in] BYTE* pbData
&dwDataLen, // [in] DWORD* pdwDataLen
lstrlen(szData) // [in] DWORD dwBufLen
))
{
HandleError("Error during CryptEncrypt!\n");
return 0;
}//end if
printf("\ndwDataLen is: %d\n\n", dwDataLen);
printf("\nszData is: %s\n", szData);
if(! CryptDecrypt( hDESKey, // HCRYPTKEY hKey
0, // HCRYPTHASH hHash
FALSE, // BOOL Final
0, // DWORD dwFlags
(LPBYTE)szData, // BYTE* pbData
&dwDataLen)) // DWORD* pdwDataLen
{
HandleError("Error during CryptDecrypt!\n");
return 0;
}
printf("****** DECRYPTED ********\n");
printf("\ndwDataLen is: %d\n\n", dwDataLen);
printf("\nszData decrypted is: %s\n\n", szData);
return 0;
}
// -----------------------------------------------------------
void StrToByte( DWORD _dwLen, LPTSTR _pStr, LPBYTE _pByte)
{
__int8 iTempVal = 0;
for(DWORD i = 0; i < _dwLen; i++)
{
if(!((iTempVal = _pStr[i] - '0') <= 9))
{
iTempVal = _pStr[i] - 55;
}
*_pByte = iTempVal << 4;
if(!((iTempVal = _pStr[++i] - '0') <= 9))
{
iTempVal = _pStr[i] - 55;
}
*_pByte |= iTempVal;
_pByte++;
}
}//end StrToByte().
void ByteToStr(DWORD cb, void* pv, LPTSTR sz)
{
//---------------------
// Parameters passed are:
// pv is the array of BYTES to be converted.
// cb is the number of BYTEs in the array.
// sz is a pointer to the string to be returned.
//---------------------
// Declare and initialize local variables.
BYTE* pb = (BYTE*) pv; // local pointer to a BYTE in the BYTE array
DWORD i; // local loop counter
int b; // local variable
//---------------------
// Begin processing loop.
for (i = 0; i<cb; i++)
{
b = (*pb & 0xF0) >> 4;
*sz++ = (b <= 9) ? b + '0' : (b - 10) + 'A';
b = *pb & 0x0F;
*sz++ = (b <= 9) ? b + '0' : (b - 10) + 'A';
pb++;
}
*sz++ = 0;
return;
}//end ByteToStr()
void HandleError(char *s)
{
cout << "\n" << s << endl;
printf("Error number %x\n.",GetLastError());
cout << "Program terminating.\n\n";
return;
}

