Hi all,
First of all props to Randor for helping me and giving me a base code now I modified his code and this is it:
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <Wincrypt.h>
#include <Xenroll.h>
#include <atlenc.h>
#include <atlcrypt.h>
#pragma comment(lib, "crypt32.lib")//n7M41mbWvrA=
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;
CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
CryptHashData(hHash,(BYTE *)passwd,pwlen,0);
CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
DWORD dMode = CRYPT_MODE_ECB;
CryptSetKeyParam(hKey, PKCS5_PADDING, reinterpret_cast<const BYTE *>(&dMode), 0);
if(*ptlen >= ctlen)
{
memcpy(plaintext,cyphertext,*ptlen);
BOOL result = CryptDecrypt(hKey,NULL,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);
dwUnEncryptedSize = NULL;
TCHAR *decoded = reinterpret_cast<TCHAR*>(decodedStr);
dwSizeNeeded = TripleDESdecrypt2(decoded,MAX_PATH * sizeof(TCHAR),szKey,_tcslen(szKey),decodedStr,&dwUnEncryptedSize);
psz3DESDecoded = new TCHAR[dwSizeNeeded];
TripleDESdecrypt2(decoded,dwSizeNeeded,szKey,_tcslen(szKey),decodedStr,&dwSizeNeeded);
pszBase64 = Base64Encode2(decodedStr,BufSize,FALSE, &BufSize);
delete [] pszBase64;
delete [] pszBase64Decoded;
delete [] pszEncrypted;
return 0;
}
Now this is a long version of the decryption method of this:
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.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.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
Now I am able to encode a string to base 64 and get its original string bace by using Base64Encode2 and Base64Decode2. My goal now is to decrypt this string "n7M41mbWvrA=" into the string "test". Thing is CryptDecrypt fails and gives me a bad data. What could be possibly wrong with me.
Thanks,
Jayjay
|