Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

DES encryption using CryptoAPIs

This site came up when I was searching the net for info about working with DES and cryptoapi's. There isn't much info out there it seems.

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;
}
[2530 byte] By [hazweb] at [2007-11-11 10:22:20]
# 1 Re: DES encryption using CryptoAPIs
Hey haz;
what about posting this in security forum better ? u may have good answers there from security interested ppls ..

Well about you "API" method , I haven't used it before and I don't know what's wrong , but I see hashing and creat hash .. , maybe this is for making an authetication for web application for example and not giving the correct encrypted text ...
try for xample removing those two hash functions , maybe this is the problem .

Otherwise I have an old -c based- code and I can send it to u if you r intersted , it contains the whole algorithm and description of all statments .. and I have tested it now it gives me your correct answers .
But just try first to see what happens when removing the two hash functions maybe this works .
Amahdy at 2007-11-11 20:58:56 >
# 2 Re: DES encryption using CryptoAPIs
Cheers, ty for responce. I thought that as well about signing, since MD5 hash. I'll look into it cheers.

I also have some c++ source that does it, but I want to get these cryptoapi's to bend to my will. :rolleyes:
hazweb at 2007-11-11 20:59:57 >