Re: How good an encryption algorithm is this?

From: Roy Fine (rlfine_at_twt.obfuscate.net)
Date: 11/26/04


Date: Fri, 26 Nov 2004 14:40:04 -0500

Bonj,

if you want to go back to using the Crypto API - I reworked your code - and
now it encrypts and decrypts with no loss of data. here is the code:

/* ************************************************ */

#pragma comment(lib,"Advapi32.lib")

char *szPassword = "eieio";

/* ------------------------------------------------ */
BOOL GetData(_TCHAR* datain, long lendatain)
{
 HCRYPTPROV hCryptProv;
 HCRYPTHASH hCryptHash;
 HCRYPTKEY hCryptKey;

 DWORD sts;
 BOOL bSuccess = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
0);
 bSuccess = CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hCryptHash);
 bSuccess = CryptHashData(hCryptHash, (BYTE*)szPassword,
_tcslen(szPassword), 0);
 bSuccess = CryptDeriveKey(hCryptProv, CALG_RC2, hCryptHash, 0, &hCryptKey);

 DWORD cryptBlockSize = lendatain;
 bSuccess = CryptEncrypt(hCryptKey, NULL, TRUE, 0,(BYTE *)datain,
&cryptBlockSize, 0);
 sts = GetLastError();
 BYTE* bData = new BYTE[cryptBlockSize];
 memcpy(bData, datain, lendatain);

 DWORD bytesback = lendatain;
 bSuccess = CryptEncrypt(hCryptKey, NULL, TRUE, 0, bData, &bytesback,
cryptBlockSize);
 sts = GetLastError();

 CryptDestroyKey(hCryptKey);
 CryptDestroyHash(hCryptHash);
 CryptReleaseContext(hCryptProv, 0);

 bSuccess = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
 bSuccess &= CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hCryptHash);
 bSuccess &= CryptHashData(hCryptHash, (BYTE*)szPassword,
_tcslen(szPassword), 0);
 bSuccess &= CryptDeriveKey(hCryptProv, CALG_RC2, hCryptHash, 0,
&hCryptKey);

 bSuccess &= CryptDecrypt(hCryptKey, NULL, TRUE, 0, (BYTE *)bData,
&bytesback);
 bData[bytesback] = 0;

 CryptDestroyKey(hCryptKey);
 CryptDestroyHash(hCryptHash);
 CryptReleaseContext(hCryptProv, 0);

 delete[] bData;
 return bSuccess;
}

/* ------------------------------------------------ */
void test(_TCHAR* string)
{
 GetData(string, _tcslen(string));
}

/* ------------------------------------------------ */
int _tmain(int argc, _TCHAR* argv[])
{
 test(_T("TheMagicBonj"));
 return 0;
}


Loading