| ||||
| ||||
В соответствии с примером шифрую текстовую строку (сhar str[]="asdf"), она шифруется. Но потом когда я ее расшифровываю символы в строке меняются, но в исходное состояние не возвращаются. В чем может быть проблема? HANDLE hCSPDLL=0; DWORD dwProvType = PROV_GOST_DH; VTableProvStruc vTable; HCRYPTPROV hProv = 0, hProv1=0; LPBYTE szContainer = NULL; DWORD dwFlags = 0; ALG_ID Algid; LPBYTE ClientBlob; DWORD LenClientBlob; LPBYTE CryptOpenKeyBlob; DWORD LenCryptOpenKeyBlob; LPBYTE *SecretBlob; DWORD LenSecretBlob; BYTE *pbKeyBlob; DWORD dwKeyBlobLen; char str[]="asdf"; unsigned long lenstr=4; void __fastcall TForm1::Button1Click(TObject *Sender) { HKEY hKeyS; HCRYPTKEY hSessionKey=0; HCRYPTKEY hTmpKey=0; HCRYPTKEY hKey=0; HCRYPTKEY hXchgKey=0; HCRYPTKEY hAgreeKey=0; BOOL bResult; DWORD key_length = 0; LPBYTE pbIV; LPBYTE pbSV; DWORD dwDataLen, Param; Label1->Caption="Process..."; Form1->Refresh(); if(CheckBox1->Checked) dwFlags=CRYPT_NEWKEYSET; else dwFlags=CRYPT_MACHINE_KEYSET; szContainer=Edit1->Text.c_str(); bResult=MyAcquireContext(&hProv, szContainer, dwFlags, &vTable); //áåðåì êëþ÷ îáìåíà äëÿ ýêñïîðòà bResult=MyGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey); if(GetLastError()==NTE_NO_KEY) MyGenKey(hProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hXchgKey); //ñìîòðèì ñêîëüêî áóäåò çàíèìàòü Blob è âûäåëÿåì ïàìÿòü ïîä íåãî bResult=MyExportKey(hProv, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, NULL, &LenCryptOpenKeyBlob); CryptOpenKeyBlob = (unsigned char*)malloc(LenCryptOpenKeyBlob); // ïîìåùàåì êëþ÷è â áëîá bResult=MyExportKey(hProv, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, CryptOpenKeyBlob , &LenCryptOpenKeyBlob); // ãåíåðèðóåì êëþ÷ ñåññèè bResult=MyGenKey(hProv, CALG_G28147 ,CRYPT_EXPORTABLE ,&hSessionKey); bResult=MyImportKey(hProv, ClientBlob, LenClientBlob, hXchgKey, 0, &hAgreeKey); //ñìîòðèì ñêîëüêî áóäåò çàíèìàòü Blob è âûäåëÿåì ïàìÿòü ïîä íåãî bResult=MyExportKey(hProv, hSessionKey, hAgreeKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen); pbKeyBlob = (unsigned char*)malloc(dwKeyBlobLen); // ïîìåùàåì êëþ÷è â áëîá bResult=MyExportKey(hProv, hSessionKey, hAgreeKey, SIMPLEBLOB, 0, pbKeyBlob , &dwKeyBlobLen); SecretBlob=&pbKeyBlob; LenSecretBlob=dwKeyBlobLen; bResult=MyEncrypt(hProv, hSessionKey, 0, 1, 0, str, &lenstr, 5); if(GetLastError()==NTE_BAD_ALGID) lenstr=1; Label1->Caption="OK"; Label2->Caption=(AnsiString)str; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { HCRYPTKEY hSessionKey=0; HCRYPTKEY hTmpKey=0; HCRYPTKEY hKey=0; HCRYPTKEY hXchgKey=0; HCRYPTKEY hAgreeKey=0; BYTE *pbKeyBlob; DWORD dwKeyBlobLen; BOOL bResult; DWORD key_length = 0; LPBYTE pbIV; LPBYTE pbSV; DWORD dwDataLen, Param; Label1->Caption="Process..."; Form1->Refresh(); if(CheckBox2->Checked) dwFlags=CRYPT_NEWKEYSET; else dwFlags=CRYPT_MACHINE_KEYSET; szContainer=Edit2->Text.c_str(); bResult=MyAcquireContext(&hProv1, szContainer, dwFlags, &vTable); //áåðåì êëþ÷ äëÿ ýêñïîðòà bResult=MyGetUserKey(hProv1, AT_KEYEXCHANGE, &hXchgKey); if(GetLastError()==NTE_NO_KEY) MyGenKey(hProv1, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hXchgKey); //ñìîòðèì ñêîëüêî áóäåò çàíèìàòü Blob è âûäåëÿåì ïàìÿòü ïîä íåãî bResult=MyExportKey(hProv1, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, NULL, &LenClientBlob); ClientBlob = (unsigned char*)malloc(LenClientBlob); // ïîìåùàåì êëþ÷è â áëîá bResult=MyExportKey(hProv1, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, ClientBlob , &LenClientBlob); Label1->Caption="OK"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { HCRYPTKEY hSessionKey=0; HCRYPTKEY hTmpKey=0; HCRYPTKEY hKey=0; HCRYPTKEY hXchgKey=0; HCRYPTKEY hAgreeKey=0; BYTE *pbTmpKeyBlob; DWORD dwTmpKeyBlobLen; BOOL bResult; /* ??? ????????*/ DWORD key_length = 0; LPBYTE pbIV; LPBYTE pbSV; DWORD dwDataLen, Param; Label1->Caption="Process..."; Form1->Refresh(); //áåðåì êëþ÷ äëÿ ýêñïîðòà bResult=MyGetUserKey(hProv1, AT_KEYEXCHANGE, &hXchgKey); //ñìîòðèì ñêîëüêî áóäåò çàíèìàòü Blob è âûäåëÿåì ïàìÿòü ïîä íåãî //ñìîòðèì ñêîëüêî áóäåò çàíèìàòü Blob è âûäåëÿåì ïàìÿòü ïîä íåãî /* bResult=MyExportKey(hProv1, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, NULL, &dwTmpKeyBlobLen); pbKeyBlob = (unsigned char*)malloc(dwKeyBlobLen); // ïîìåùàåì êëþ÷è â áëîá bResult=MyExportKey(hProv1, hXchgKey, hTmpKey, PUBLICKEYBLOB, 0, pbTmpKeyBlob , &dwTmpKeyBlobLen); */ bResult=MyImportKey(hProv1, CryptOpenKeyBlob, LenCryptOpenKeyBlob, hXchgKey, 0, &hAgreeKey); bResult=MyImportKey(hProv1, pbKeyBlob, dwKeyBlobLen, hAgreeKey, 0, &hSessionKey ); if(GetLastError()==NTE_BAD_KEY) lenstr=1; if(GetLastError()==NTE_BAD_TYPE) lenstr=1; if(GetLastError()==NTE_BAD_DATA) lenstr=1; if(GetLastError()==NTE_BAD_FLAGS) lenstr=1; if(GetLastError()==NTE_PERM) lenstr=1; bResult=MyDecrypt(hProv1, hSessionKey, 0, 1,0, str, &lenstr ); } //--------------------------------------------------------------------------- |