Hi there,
I need a c++ version of the decryption so far my attempts have failed this is my code:
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 TripleDESdecrypt2(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,BYTE *plaintext,DWORD *ptlen)
{
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
DWORD dwSizeNeeded =0;
BYTE hashData;
BLOBHEADER stBlob;
stBlob.bType = PLAINTEXTKEYBLOB;
stBlob.aiKeyAlg = CALG_3DES;
stBlob.reserved = 0;
stBlob.bVersion = 0x02;
BOOL result = CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
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,hHash,1,0,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[MAX_PATH * sizeof(TCHAR)] = _T("n7M41mbWvrA=");
TCHAR * pszEncrypted = NULL;
TCHAR * pszBase64 = NULL;
TCHAR * pszBase64Decoded = NULL;
TCHAR * psz3DESDecoded = NULL;
DWORD dwSizeNeeded;
DWORD BufSize;
DWORD dwUnEncryptedSize;
TCHAR * KeyBase64 = NULL;
decodedStr = Base64Decode2(szUnencrypted,_tcslen(szUnencrypted) * sizeof(TCHAR), &BufSize);
TCHAR* NewDecoded = reinterpret_cast<TCHAR*>(decodedStr);
dwUnEncryptedSize = NULL;
TCHAR *decoded = NewDecoded;
dwSizeNeeded = TripleDESdecrypt2(decoded,BufSize,szKey,_tcslen(szKey),decodedStr,&dwUnEncryptedSize);
psz3DESDecoded = new TCHAR[dwSizeNeeded];
TripleDESdecrypt2(decoded,BufSize,szKey,_tcslen(szKey),decodedStr,&dwSizeNeeded);
pszBase64 = Base64Encode2(decodedStr,BufSize,FALSE, &BufSize);
delete [] pszBase64;
delete [] pszBase64Decoded;
delete [] pszEncrypted;
return 0;
}
And by the way i have slightly modified how I encrypt by c#:
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
HashAlgorithm XHash = HashAlgorithm.Create("MD5");
keyArray = XHash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
XHash.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
Please help me
Many thanks,
Jayjay
|