DES encryption using CryptoAPIs
I'm working in VC++ 6 and trying to encrypt a string using just plain old DES. The code encrypts the string ok, but its not the correct value I should be getting. :(
I'm using this DES calculator to check my output and it is where the correct output came from and from other sources I know it is valid.
http://www.adfa.oz.au/~lpb/src/DEScalc/DEScalc.html
Input: 3031323334353637 // "01234567"
Key: 74657374696E6731 // "testing1"
Correct out: e1f077a034ebdf8e
Output CryptoAPI's give: ceae0b0456d90e0d
I used this post somewhat as a template along with one other code sample I found:
http://forums.dev-archive.com/showthread.php?t=34803
Not sure whats going wrong. :SICK:
// destest.cpp
#pragma comment(lib, "advapi32.lib")
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <conio.h>
HCRYPTPROV hCryptProv;
HCRYPTKEY hDESKey;
HCRYPTHASH hHash;
#define KEY_LENGTH 0x00400000
int crypto_startup() {
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
return FALSE;
return TRUE;
}
void crypto_wrapup() {
if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
hCryptProv = 0;
}
BOOL DES_Encrypt(LPBYTE bData, LPBYTE bKey, DWORD dwKeyLen ){
if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) // Create a hash object.
return FALSE;
if(!CryptHashData(hHash, bKey, dwKeyLen, 0)) // Call CryptHashData.
return FALSE;
if(!CryptDeriveKey(hCryptProv, CALG_DES, hHash, KEY_LENGTH, &hDESKey))
return FALSE;
if(!CryptEncrypt(hDESKey, 0, FALSE, 0, bData, &dwKeyLen, 8)) // correct value: e1f077a034ebdf8e
return FALSE;
return TRUE;
}
int main(int argc, char *argv[])
{
printf("Crypt Test\n\n");
if(crypto_startup()){
BYTE bKey[9] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x00 }; // Encryption/Decryption Key ; 74657374696E6731
BYTE bData[9] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x00 }; // Data to be Encrypted ; 3031323334353637
DWORD dwDataLen = 8; // Size of Key
DES_Encrypt(bData, bKey, dwDataLen);
}
return 0;
}

