DES gives wrong output. why???

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am using Visual C++ to implement DES algorithm using CryptoAPI.
I implemented using it. But it is giving wrong output.
Why it is happening??


#include <windows.h>
#include <wincrypt.h>
#include <dos.h>
#include <conio.h>
#include <iostream>
using namespace std;

#define MAX_BLOB 64
#define KEY_LENGTH 8
BYTE bPlainText1[MAX_BLOB];
BYTE bPlainText2[MAX_BLOB];
BYTE bCipherText1[MAX_BLOB];
BYTE bCipherText2[MAX_BLOB];
BYTE bKeyStream[MAX_BLOB];
BYTE bKey[MAX_BLOB];

BOOL DES_Encrypt(LPBYTE bData, LPBYTE bKey, DWORD dwKeyLen )
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;

if(!(CryptAcquireContext(&hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, 0))
return FALSE;
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return FALSE;
if(!CryptHashData(hHash, bKey, dwKeyLen, 0))
return FALSE;
if(!CryptDeriveKey(hProv, CALG_DES, hHash, CRYPT_EXPORTABLE, &hKey))
return FALSE;
if(!CryptEncrypt(hKey, 0, FALSE, 0, bData, &dwKeyLen, 8))
return FALSE;

if (hKey) CryptDestroyKey(hKey);
if (hHash) CryptDestroyHash(hHash);
if (hProv) CryptReleaseContext(hProv, 0);
return TRUE;
}


int main(int argc, char *argv[])
{
printf("Crypt Testnn");
BYTE bKeyTemp[9],bDataTemp[9];
BYTE bKey[9][9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
BYTE bData[9][9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

for(int i=0;i<9;i++)
{
bDataTemp=bData[0];
bKeyTemp=bKey[0];}
DWORD dwDataLen = 8; // Size of Key
printf("CALLING DES FUNn");
DES_Encrypt(bDataTemp, bKeyTemp, dwDataLen);
getch();
return 0;
}
The Test case for algorithm are
Key Plaintext Ciphertext0000000000000000 0000000000000000 8CA64DE9C1B123A7
FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 7359B2163E4EDC58
3000000000000000 1000000000000001 958E6E627A05557B
1111111111111111 1111111111111111 F40379AB9E0EC533
0123456789ABCDEF 1111111111111111 17668DFC7292532D
1111111111111111 0123456789ABCDEF 8A5AE1F81AB8F2DD
0000000000000000 0000000000000000 8CA64DE9C1B123A7
FEDCBA9876543210 0123456789ABCDEF ED39D950FA74BCC4
7CA110454A1A6E57 01A1D6D039776742 690F5B0D9A26939B
For first test case output given by this program is

E6 D6 DB 5E 45 c7 A0 D8
However it should give as "8CA64DE9C1B123A7".
I am not getting why MD5 is used to hash and what is purpose of Hash.
I already read msdn support information for this API.

Please correct the program if mistaken. Thank you.

View the full article
 
Back
Top