This is my code:
It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA==
Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link
LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
{
DWORD dwResult = 0;
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
{
LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
memset(lpszBase64Decoded,0,dwResult);
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
{
*(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;
return lpszBase64Decoded;
}
}
return NULL;
}
LPTSTR Base64Encode(LPCBYTE lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwBufSize)
{
DWORD dwResult = 0;
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult))
{
LPTSTR lpszBase64 = new TCHAR[dwResult];
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, lpszBase64, &dwResult))
{
TCHAR pByteLF = *(LPWORD)(lpszBase64 + dwResult -1);
TCHAR pByteCR = *(LPWORD)(lpszBase64 + dwResult -2);
if(pByteCR == 0x0D && pByteLF == 0x0A)
{
*(LPWORD)(lpszBase64 + dwResult -2) = 0;
}
*dwBufSize = dwResult;
return lpszBase64;
}
}
return NULL;
}
BYTE* Base64Decode2(LPCTSTR lpData, DWORD dwSize, DWORD *dwBufSize)
{
DWORD dwResult = 0;
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
{
BYTE * decodedStr;
decodedStr = new BYTE;
LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
memset(decodedStr,0,dwResult);
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,decodedStr, &dwResult,NULL,NULL))
{
*dwBufSize = dwResult;
return decodedStr;
}
}
return NULL;
}
LPTSTR Base64Encode2(BYTE* lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwbufSize)
{
DWORD dwResult = 0;
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult))
{
LPTSTR lpszBase64 = new TCHAR[dwResult];
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, lpszBase64, &dwResult))
{
TCHAR pByteLF = *(LPWORD)(lpszBase64 + dwResult -1);
TCHAR pByteCR = *(LPWORD)(lpszBase64 + dwResult -2);
if(pByteCR == 0x0D && pByteLF == 0x0A)
{
*(LPWORD)(lpszBase64 + dwResult -2) = 0;
}
*dwbufSize = dwResult;
return lpszBase64;
}
}
return NULL;
}
DWORD TripleDESdecrypt(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,TCHAR *plaintext,DWORD *ptlen)
{
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
DWORD dwSizeNeeded =0;
BOOL result = CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
result = CryptHashData(hHash,(BYTE *)passwd,pwlen,0);
result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
BYTE PadMode = CRYPT_MODE_ECB;
result = CryptSetKeyParam(hKey, PKCS5_PADDING, &PadMode, 0);
if(*ptlen >= ctlen)
{
memcpy(plaintext,cyphertext,*ptlen);
result = CryptDecrypt(hKey,NULL,1,0,(BYTE *)plaintext,&ctlen);
*ptlen=ctlen;
}
else
{
dwSizeNeeded = ctlen * sizeof(TCHAR);
}
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
CryptReleaseContext(hProv,0);
return dwSizeNeeded;
}
int main()
{
BYTE* decodedStr;
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash = 0;
HCRYPTKEY hKey = 0;
TCHAR szKey[] = _T("h3bmull3r");
TCHAR szUnencrypted[] = _T("");
TCHAR * pszEncrypted = NULL;
TCHAR * pszBase64 = NULL;
TCHAR * pszBase64Decoded = NULL;
TCHAR * psz3DESDecoded = NULL;
DWORD dwSizeNeeded;
DWORD BufSize;
DWORD dwUnEncryptedSize;
DWORD dwEncryptedSize;
DWORD KeySize;
TCHAR * NewKey = Base64Encode((LPCBYTE)&szKey,_tcslen(szKey) * sizeof(TCHAR),FALSE, &KeySize);
_tcscpy(szKey, NewKey);
pszBase64 = _T("bZVLVixsjgTxsWbwmAGitRR0T86AM9Kam4BYRR+Dukc9p9vhwUBAzCSlZ463ZInufJRE+7ridImlz/aJUJbVhg==");
BYTE* TEST = Base64Decode2(pszBase64,_tcslen(pszBase64) * sizeof(TCHAR), &dwEncryptedSize);
TCHAR * pszEncrypted2 = reinterpret_cast<TCHAR *>(TEST);
pszEncrypted = pszEncrypted2;
dwUnEncryptedSize = NULL;
dwSizeNeeded = TripleDESdecrypt(pszEncrypted,dwEncryptedSize * sizeof(TCHAR),szKey,_tcslen(szKey),szUnencrypted,&dwUnEncryptedSize);
psz3DESDecoded = new TCHAR[dwSizeNeeded];
TripleDESdecrypt(pszEncrypted,dwEncryptedSize * sizeof(TCHAR),szKey,_tcslen(szKey),psz3DESDecoded,&dwSizeNeeded);
pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));
\
MessageBox(NULL,0 == _tcscmp(pszBase64Decoded,szUnencrypted) ? _T("Strings Match"):_T("Strings do NOT Match"),_T(""),MB_OK);
delete [] pszBase64;
delete [] pszBase64Decoded;
delete [] pszEncrypted;
return 0;
}
My code is a c++ version of this code : http://www.codeproject.com/KB/web-security/Cryptography_MD5_TriDES.aspx?display=Print[^]
Please help me I really need this I did all what I can.
Thanks,
Jayjay
|