|
Hey Jim,
jkirkerx wrote: I give up.
Don't give up so easily man.
Lets step back and summarize your problem:
1.) You have implemented some encryption in ASP.NET and also VB.NET and you want to duplicate the encryption within C++ using the Microsoft cryptography provider.
2.) You have found that the encrypted messages do not match.
3.) The .NET framework implements its own provider that seems to do things a bit differently than the default Microsoft cryptographic service provider (which is what c++ will utilize).
Have you considered approaching the problem from the other side? Rather than attempt to duplicate the .NET cryptography within your C++ application. You could make native API calls from your .NET application and duplicate the C++ encryption code. The .NET framework is perfectly capable of making P/Invoke native API calls and this would ensure that all 3 code bases were the same.
Best Wishes,
-David Delaune
|
|
|
|
|
I considered that this morning. I made a shout out on another forum, and Igor replied this morning.
Your VB code isn't hashing or deriving anything. You are likely looking for CryptImportKey. That's how you obtain HCRYPTKEY handle from raw key material.
PKCS 5 and PKCS 7 specify the same padding algorithm. Your problem has nothing to do with padding, and is most likely due to the fact that you are encrypting with different keys.
Then there was that observation I made, but I'm not sure what to think of it. VB said I used this set of keys, and the key I used are on the bottom. I tried both sets, and I get different responses.
BYTE KEY_64[8] = {42, 16, 93, 156, 78, 4, 218, 32};
BYTE IV_64[8] = {55, 103, 246, 79, 36, 99, 167, 3};
Food for thought, just not sure how to proceed.
|
|
|
|
|
Jim,
jkirkerx wrote: Your VB code isn't hashing or deriving anything. You are likely looking for CryptImportKey. That's how you obtain HCRYPTKEY handle from raw key material.
I'm not understanding what your friend Igor is saying here... unless he is referring to something VB.NET/ASP.NET related...
jkirkerx wrote: PKCS 5 and PKCS 7 specify the same padding algorithm. Your problem has nothing to do with padding, and is most likely due to the fact that you are encrypting with different keys
It looks like I came to the same conclusion back in the year 2008: Re: CryptDecrypt Failure please help[^]
jkirkerx wrote: Food for thought, just not sure how to proceed.
Well, what I stated earlier is completely correct... if ALL of the settings are the same... padding, mode, block size etc... the results should be the same. At this point you just need to figure out what is different. Unfortunately I do not even have the .NET framework installed on any of by workstations... and when I install Visual Studio I always exclude the .NET stuff... otherwise I would be able to help identify the differences.
Best Wishes,
-David Delaune
|
|
|
|
|
I think he's saying to toss all the hash stuff, and import the key I hard coded. So instead of importing let's say a certificate key from file, import my hard coded key instead.
Then ignore what vb is doing and just trust it.
Toss this,
bResult = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
bResult = CryptHashData(hHash, KEY_64, sizeof(KEY_64), 0);
And Import Raw Material
bResult = CryptImportKey(hProv, KEY_64, sizeof(KEY_64), &hKey, 0, &hKey);
|
|
|
|
|
This was my interpretation of the advice. I'll take it for a test run, if good, I need to figure out how to code the key.
BYTE DesKeyBlob_64[] = {
0x08,0x02,0x00,0x00,0x01,0x66,0x00,0x00,
0x08,0x00,0x00,0x00,
0xf1,0x0e,0x25,0x7c,0x6b,0xce,0x0d,0x34
};
BYTE KEY_64[8] = {42, 16, 93, 156, 78, 4, 218, 32};
BYTE IV_64[8] = {55, 103, 246, 79, 36, 99, 167, 3};
<pre>
bResult = CryptAcquireContextW( &hProv, 0, MS_ENHANCED_PROV, PROV_RSA_FULL, 0);
bResult = CryptImportKey(hProv, DesKeyBlob_64, sizeof(DesKeyBlob_64), 0, CRYPT_EXPORTABLE, &hKey);
bResult = CryptSetKeyParam(hKey, KP_ALGID, (BYTE*)CALG_DES, 0);
bResult = CryptSetKeyParam(hKey, KP_IV, IV_64, 0);
|
|
|
|
|
jkirkerx wrote: BYTE DesKeyBlob_64[] = {
0x08,0x02,0x00,0x00,0x01,0x66,0x00,0x00, // BLOB header
0x08,0x00,0x00,0x00, // key length, in bytes
0xf1,0x0e,0x25,0x7c,0x6b,0xce,0x0d,0x34 // DES key with parity
};
Where did this come from? The source code that you sent me privately via e-mail does not contain a key blob. If your ASP.NET and VB.NET projects are using key blobs... then that changes everything...
-Dave
|
|
|
|
|
It came from a Microsoft MSDN description of CryptImportKey, saying that either use CryptDeriveKey, Gen Key or import Key.
I tried it because I was not able to use my existing key format in CryptImportKey, and it expected a Key Blob. So I just wanted to see if I could create a key without using CryptCreateHash.
Well it loads without error, the call to DES is suppose to be in the blob header I think. The key is not right but I did create a key without CryptCreateHash.
|
|
|
|
|
Perfect Match!, on the buffer value, and the base64 output. This is the framework for duplicating the asp.net DES and 3DES.
I need to clean it up, secure it, and take out the garbage to finalize it.
WOW, what a long journey, Thank you very much Dave for helping me out, and standing by my side during the journey, and not giving up on me.
#pragma comment (lib, "advapi32")
#pragma comment(lib, "crypt32.lib")
BYTE DesKeyBlob_64[] = {
0x08,0x02,0x00,0x00,0x01,0x66,0x00,0x00,
0x08,0x00,0x00,0x00,
0x2a,0x10,0x5D,0x9C,0x4E,0x4,0xDA,0x20
};
WCHAR* _encrypt_DES( WCHAR *pzInputW )
{
BOOL bResult = FALSE;
WCHAR *szOutput = NULL;
HCRYPTPROV hProv;
HCRYPTKEY hKey;
ALG_ID Algid;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
BYTE *pbBuffer;
DWORD dwBlockLen = 0;
DWORD dwDataLen = 0;
DWORD errorCode = 0;
DWORD cbData = 0;
BOOL bFinal = TRUE;
int iCharA = WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, NULL, 0, NULL, NULL);
char *szInputA = new char[iCharA];
WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, szInputA, iCharA, NULL, NULL);
bResult = CryptAcquireContextW( &hProv, 0, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
bResult = CryptImportKey(hProv, DesKeyBlob_64, sizeof(DesKeyBlob_64), 0, CRYPT_EXPORTABLE, &hKey);
bResult = CryptSetKeyParam(hKey, KP_IV, IV_64, 0);
dwDataLen = sizeof(ALG_ID);
cbData = sizeof(szInputA);
if (!CryptGetKeyParam(hKey, KP_ALGID, (BYTE *)&Algid, &dwDataLen, 0))
return 0;
if (GET_ALG_TYPE(Algid) != ALG_TYPE_STREAM) {
dwDataLen = sizeof(DWORD);
if (!CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE *)&dwBlockLen, &dwDataLen, 0))
return 0;
dwDataLen = ((cbData + dwBlockLen - 1) / dwBlockLen) * dwBlockLen;
if (!(pbBuffer = (BYTE *)LocalAlloc(LMEM_FIXED, dwDataLen)))
return 0;
}
else {
if (!(pbBuffer = (BYTE *)LocalAlloc(LMEM_FIXED, cbData)))
return 0;
}
CopyMemory(pbBuffer, szInputA, cbData);
if (!CryptEncrypt(hKey, 0, bFinal, 0, pbBuffer, &cbData, dwDataLen)) {
DWORD dwError = GetLastError();
if(ERROR_MORE_DATA == dwError) {
DWORD dwSizeNeeded = cbData;
CopyMemory( pbBuffer, szInputA, cbData );
bResult = CryptEncrypt(hKey, 0, FALSE, 0, pbBuffer, &dwSizeNeeded, dwDataLen);
}
LocalFree(pbBuffer);
return 0;
}
BYTE szByte[8];
for (int i = 0; i < cbData; ++i) {
szByte[i] = (BYTE)pbBuffer[i];
}
DWORD dwResult = 0;
TCHAR *szBase64 = NULL;
if(CryptBinaryToString((BYTE*)pbBuffer, 8, CRYPT_STRING_BASE64, NULL, &dwResult)) {
szBase64 = new TCHAR[dwResult];
CryptBinaryToString((BYTE*)pbBuffer, 8, CRYPT_STRING_BASE64, szBase64, &dwResult);
szOutput = (WCHAR*)szBase64;
delete [] szBase64;
}
SecureZeroMemory( pbBuffer, sizeof(pbBuffer) );
delete [] szInputA;
CryptDestroyKey(hKey);
CryptReleaseContext(hProv, 0);
return szOutput;
}
|
|
|
|
|
Hey Jim,
That is excellent news!
Just in time for Monday night football too. Since your over there in Huntington Beach, CA would that make you a San Francisco 49ers fan or San Diego Chargers fan? I'll be pulling for the Pittsburgh Steelers tonight... because if San Francisco loses the New Orleans Saints will have a chance to gain the #2 NFC slot and obtain the bye week.
Best Wishes,
-David Delaune
|
|
|
|
|
Chargers Fan, but they should of kept Marty Schottenheimer. The current coach did nothing but get good players injured over the last few years (LT).
Pittsburgh is the luckiest team I've ever seen, or perhaps I should say conservative but effective.
Ben knows what to do to win games, and the EST factor, east coast team playing on west coast should not be an issue. Without the December mud of Heines field, should be a close game.
I have bowling tonight, but will try to catch parts of the game.
|
|
|
|
|
That did fly way over my head.
So I don't need that line of code. I can just go straight to the next lines
bResult = CryptHashData(hHash, KEY_64, cbKey64, 0); <-- So this must be wrong as well
bResult = CryptDeriveKey(hProv, CALG_DES, hHash, 0, &hKey);
bResult = CryptSetKeyParam(hKey, KP_IV, IV_64, 0);
|
|
|
|
|
My function works good, only I don't get the exact value or match I expect.
I read up on Initialization vector, and used CyrptSetKeyParam to add my IV, but this occurs in the background, and you can't really check it, to make sure that's what happened.
If the IV is just a prefix, that goes in front of the data to be encrypted, then is it common practice to skip the CryptSetKeyParam, and just prefix the data to be encrypted?
I remarked out the CryptSetKeyParam, and I did get different values returned in the buffer, but not enough to get the results I'm looking for.
If I experiment with it, then I need to write a whole new buffer routine that adds the extra space, and do new calculations.
Without:
I˜a‚ú|¢-Øø×"©¨Nþt¾dÃMN££Ð¢æÀDµ£N»£jŽg5„Äþti«L6±†à©Cþìü
With:
€&§¶Í>ªÈÇ3,í&%®•Ï¢?¾(ÞÆø0Ç6Ÿ§ìÆ·(õæd¢‡ôKFo{ü‘ (!ÛÏß·Èh‰¶
|
|
|
|
|
IV is not exactly a prefix.
basically an IV is a block of data that goes through the cipher before your data does, in order to set the internal state of the cipher to something other than the default. it differs from a simple prefix in that the IV should be unpredictable to potential eavesdroppers and should be chose anew for each message. in a way, it's a secondary key.
take a look at wiki[^]
|
|
|
|
|
Can any one post some good links to C/C++ Interview questions that were asked in any of the MNC's?
|
|
|
|
|
there is a site which can give you all information. Click on this link [^]
Every new day is another chance to change your life.
|
|
|
|
|
Hi all,
#define SAFERELEASE( X ) { if( X ) { X->Release(); X = NULL; } }
HRESULT m_objHRsult;
IGraphBuilder* m_objGraphBuilder;
IMediaControl* m_objMediaControl;
IMediaEventEx* m_objMediaEvent;
IVideoWindow* m_objVideownd;
IMediaSeeking* m_objMediaSeeking;
m_objHRsult=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void**)& m_objGraphBuilder);
m_objHRsult=m_objGraphBuilder->QueryInterface(IID_IMediaControl,(void**)&m_objMediaControl);
m_objHRsult=m_objGraphBuilder->QueryInterface(IID_IMediaEventEx,(void**)&m_objMediaEvent);
m_objHRsult=m_objGraphBuilder->QueryInterface(IID_IVideoWindow,(void**)&m_objVideownd);
m_objHRsult=m_objGraphBuilder->QueryInterface(IID_IMediaSeeking,(void**)&m_objMediaSeeking);
m_objHRsult=m_objMediaEvent->SetNotifyWindow((OAHWND)main_Dlg->GetSafeHwnd(),WM_GRAPHNOTIFY,0);
m_objGraphBuilder->RenderFile(path,NULL);
m_objMediaControl->Run();
if(m_objMediaControl)
m_objMediaControl->Stop();
SAFERELEASE(m_objMediaEvent);
SAFERELEASE(m_objMediaControl);
SAFERELEASE(m_objVideownd);
SAFERELEASE(m_objGraphBuilder);
please help me for this.
thanks in advance.
|
|
|
|
|
Why didn't you release the MediaSeeking interface?
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]
|
|
|
|
|
ok not i release MediaSeeking interface also but there is some memory leak.
after RenderFile memory increase and not come down on previous positioin after releasing all interfaces.
|
|
|
|
|
I wrote an application in Visual C++ language, mfc apllication (Dialog Based, MFC in static library), PocketPC 2003 SDK to connect to *.sdf file saved in my PocketPC device.
The same code, for Windows works. The SQL querries from my mfc-Windows affetcs *.sdf file witch is savd on My Computer. But if I wrote the same code for Pocket PC to affect a file witch is saved on device it has no effect.
The code is this:
// ADOWin7Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ADOWin7.h"
#include "ADOWin7Dlg.h"
#import <<c:\\program files\\common="" files\\system\\ado\\msado15.dll="">> rename( "EOF", "AdoNSEOF")
_bstr_t bstrConnect = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\MyDatabase1.sdf;";
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
-------------ussualy code generated by Visual Sdudio------------------
void CADOWin7Dlg::OnBnClickedButton1()
{
ADODB::_ConnectionPtr pConn;
pConn.CreateInstance(__uuidof(ADODB::Connection));
pConn->Open(bstrConnect, "", "", 0);
pConn->Execute("INSERT INTO Table1 (Nume) Values ('Arad')", NULL, NULL);
pConn->Close();
}
I cann not understand why the code works on PC and the same code does not work on Pocket PC. If I run the *.exe file on Pocket PC does not appears any error or message. If I run Query Analyzer 3.5 what is installed on Pocket PC, and make an SQL querry from it, it affects my *sdf file from device. The SQL Compact Edition is installed on device.
Maybe is an ADO connection problem. I don't know.
Thanks for help.
modified 19-Dec-11 11:42am.
|
|
|
|
|
Adding some debug code, or at the very least, checking the return values from your function calls would probably give you some useful information.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
hello
I want to ask about the library for otimization matrix.
Anyone know about library for optimization matrix that can use in visual c++?
Because I need the library that can solve the optimization matrix like a cvx_solver library in matlab.
Thanks so much
|
|
|
|
|
|
After I read that link, I think all of the library is for matlab. But I need to solve the optimization matrix in visual c++. The sample matlab source that I want to solve in visual c++ is
cvx_begin
variable x(n)
minimize( norm( A * x - b, 2 ) )
subject to
C * x == d
norm( x, Inf ) <= e
cvx_end
Anyone know the library for visual c++ to solve that problem?
Thank you
|
|
|
|
|
There are several links, and while some of the links offered there imply the inclusion of matlab, others don't. I haven't checked any, I just did a quick search on google and this seemed the most promising one for software solutions not including matlab. I've found one other link that pointed to Wolfram Alpha Mathematica instead - but I thought if you don't want matlab, then maybe Mathematica is not an option either.
|
|
|
|
|
How to tell the OS to use the environment provided by the WinForm application instead from system environment.
Example : My WinForm application uses C++ runtime dll's(msvcm80.dll, msvcp80.dll and msvcr80.dll) and bundling them as part of the application. How to make sure that my application will use the C++ runtime dll's that bundled along with product even though the dll's present else where in the system(c:\Windows\Winsxs).
Regards,
Gopal Reddy
|
|
|
|
|