string Test3DES()
{
string key = "12656b2e4ba2f22e";
HCRYPTPROV hCryptProv = NULL;
HCRYPTHASH hHash = NULL;
HCRYPTKEY hCryptKey = NULL;
char pIV[] = "d566gdbc"; //simple test IV for 3DES
CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
CryptCreateHash( hCryptProv, CALG_MD5, NULL, 0, &hHash );
CryptHashData( hHash, (LPBYTE)key.c_str(), (DWORD)key.size(), 0 );
DWORD dwMode = CRYPT_MODE_CBC;
CryptDeriveKey(hCryptProv, CALG_3DES, hHash, 0, &hCryptKey);
CryptSetKeyParam(hCryptKey, KP_MODE, (BYTE*)&dwMode, 0);
CryptSetKeyParam(hCryptKey, KP_IV,(const BYTE*) pIV, 0) ;
DWORD dwFilled = 0;
BOOL ret = CryptEncrypt( hCryptKey, NULL, TRUE, 0, (LPBYTE)cipherText.c_str(), &dwFilled, (DWORD)str.size());
cipherText.resize(dwFilled);
if( hCryptKey ) CryptDestroyKey( hCryptKey );
if( hHash ) CryptDestroyHash( hHash );
if( hCryptProv ) CryptReleaseContext( hCryptProv, 0 );
return cipherText;
}
3DES_112
string key = "12656b2e4ba2f22e"; unsigned char pIV[] = "d566gdbc"; //simple test IV for 3DES HCRYPTPROV hCryptProv = NULL; HCRYPTHASH hHash = NULL; HCRYPTKEY hCryptKey = NULL; DWORD ret = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,CRYPT_VERIFYCONTEXT); if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl; PlainTextKeyBlob keyBlob ={0}; keyBlob.hdr.bType = PLAINTEXTKEYBLOB; keyBlob.hdr.bVersion = CUR_BLOB_VERSION; keyBlob.hdr.reserved = 0; keyBlob.hdr.aiKeyAlg = CALG_3DES_112; keyBlob.cbKeySize = key.size(); memcpy(keyBlob.key, key.c_str(), key.size()); DWORD dwSizeBlob = sizeof(BLOBHEADER)+sizeof(DWORD)+key.size(); ret = CryptImportKey( hCryptProv, (const BYTE*)&keyBlob, dwSizeBlob, 0, CRYPT_EXPORTABLE, &hCryptKey ); if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl; DWORD dwMode = CRYPT_MODE_CBC; CryptSetKeyParam(hCryptKey, KP_MODE, (BYTE*)&dwMode, 0); CryptSetKeyParam(hCryptKey, KP_IV,(const BYTE*) pIV, 0) ; std::vector< BYTE > buffer( 1024 ); memcpy( &buffer[0], passwd.c_str(), passwd.size() ); DWORD dwFilled = passwd.size(); ret = CryptEncrypt( hCryptKey, NULL, TRUE, 0, (LPBYTE)&buffer[0], &dwFilled, (DWORD)buffer.size()); if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl; buffer.resize(dwFilled); if( hCryptKey ) CryptDestroyKey( hCryptKey ); if( hHash ) CryptDestroyHash( hHash ); if( hCryptProv ) CryptReleaseContext( hCryptProv, 0 ); return buffer;