Please help me with my decryption it almost works
-
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)( -
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)(Actually your code is failing (at least) in the following line:
monsieur_jj wrote:
pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));
(
CryptStringToBinary
returns0
. BTW: are you using the debugger?). Moreover the following comparisonmonsieur_jj wrote:
_tcscmp(pszBase64Decoded,szUnencrypted)
makes no sense, since
szUnencrypted
is a buffer never initialised. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
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)(Hi, FWIW the encoded string is probably OK, since it decodes to http://localhost:8080/MyWeb/managepoint/managepoint341.zip it took 2 lines of C# code to get that. Not sure what is wrong in your code though. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Actually your code is failing (at least) in the following line:
monsieur_jj wrote:
pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));
(
CryptStringToBinary
returns0
. BTW: are you using the debugger?). Moreover the following comparisonmonsieur_jj wrote:
_tcscmp(pszBase64Decoded,szUnencrypted)
makes no sense, since
szUnencrypted
is a buffer never initialised. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Hi Pallini, Sorry never mind the message box output i forgot to comment that. Can anyone tell me the difference between the C# code and mine? Im pretty sure pkcs5 is the same at pkcs7 and i got all inputs the same. Thanks, Jayjay
-
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)(Hi JayJay, As I stated in this post[^] I suspect your default Microsoft cryptographic service provider does not support Triple ECB. I am only speculating as I have not spent much time researching this. I suggest that you enumerate all supported algorithms on your development box. You can use the CryptEnumProviders Function[^] to see all the CSP. You can use the CryptEnumProviderTypes Function[^] to see all supported algorithms. If 3DES ECB is not listed then your CSP may not support it. Example C Program: Enumerating CSP Providers and Provider Types[^] You have spent a large amount of time trying to make this work with Microsoft API, perhaps you should begin looking at third party 3DES implementations. Here are a few I have found using Google. http://www.codeguru.com/cpp/misc/misc/cryptoapi/print.php/c8195[^] http://www.cs.auckland.ac.nz/~pgut001/cryptlib/[^] Best Wishes, -David Delaune
-
Hi JayJay, As I stated in this post[^] I suspect your default Microsoft cryptographic service provider does not support Triple ECB. I am only speculating as I have not spent much time researching this. I suggest that you enumerate all supported algorithms on your development box. You can use the CryptEnumProviders Function[^] to see all the CSP. You can use the CryptEnumProviderTypes Function[^] to see all supported algorithms. If 3DES ECB is not listed then your CSP may not support it. Example C Program: Enumerating CSP Providers and Provider Types[^] You have spent a large amount of time trying to make this work with Microsoft API, perhaps you should begin looking at third party 3DES implementations. Here are a few I have found using Google. http://www.codeguru.com/cpp/misc/misc/cryptoapi/print.php/c8195[^] http://www.cs.auckland.ac.nz/~pgut001/cryptlib/[^] Best Wishes, -David Delaune
Hi David, These are the list of what is supported:
Listing Available Providers:
Provider type Provider Name
_____________ _____________________________________
1 Gemplus GemSAFE Card CSP v1.0
1 Infineon SICRYPT Base Smart Card CSP
1 Microsoft Base Cryptographic Provider v1.0
13 Microsoft Base DSS and Diffie-Hellman Cryptographic Provider
3 Microsoft Base DSS Cryptographic Provider
18 Microsoft DH SChannel Cryptographic Provider
1 Microsoft Enhanced Cryptographic Provider v1.0
13 Microsoft Enhanced DSS and Diffie-Hellman Cryptographic Provider24 Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype
)
5 Microsoft Exchange Cryptographic Provider v1.0
12 Microsoft RSA SChannel Cryptographic Provider
1 Microsoft Strong Cryptographic Provider
1 Schlumberger Cryptographic Service ProviderProvider types and provider names have been listed.
Listing Available Provider Types:
Provider type Provider Type Name
_____________ _____________________________________
1 RSA Full (Signature and Key Exchange)
3 DSS Signature
5 Microsoft Office Outlook/Exchange Cryptographic Service Provider12 RSA SChannel 13 DSS Signature with Diffie-Hellman Key Exchange 18 Diffie-Hellman SChannel 24 RSA Full and AES
I read at msdn that Microsoft Enhanced Cryptographic Provider v1.0 supports triple DES now I am confused to what the problem really is. Thanks, Jayjay