Bonjours,

Je vous explique mon problème:
je doit décripter un fichier crypté avec l'algorithme AES 128; la clé pour décrypter se fichier est le hash sha1 d'un certain mot. j'ai donc généré ce hash sous linux avec la commande : " echo "mot" | openssl sha1"

je doit ensuite me servir de ce mot de passe et de l'api cryptAPI de windows, voici donc le code que j'ai pour l'instant:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincrypt.h>
 
 
#define buf_size 2048
 
 
 
void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code
 
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 
 
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
 
    // Display the error message and exit the process
 
    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
        printf("%s failed with error %d: %s", lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
 
    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
 
    system("PAUSE");
}
 
 
 
 
 
 
int main(int argc, char *argv[])
{
    printf("depart");
 
    char * texte[buf_size+1];
    char * texte2[buf_size+1];
    FILE* fd;
    int i=0;
    char value;
 
    char * buffer[buf_size+1]; 
 
    if( (fd=fopen("C:\\Dev-Cpp\\projects\\decriptage\\1009369384.stream","rb")) == NULL )
    {
        perror("opening failed");    
        exit(0);
    }
    memset(buffer,0,2048);
    memset(texte,0,2048);
    memset(texte2,0,2048);
 
    system("PAUSE");
 
    while( fread(&texte,1,1*sizeof(char) ,fd)== 1 ){
           //printf("%d::::\n%s",strlen(texte),texte);
           strcat(texte2,texte);
    } 
 
    //fread(texte,1,512,fd);
    strcat(texte2,texte);
 
 
    printf("::::%d::::\n%s\nfin fichier crypté.\n\n",strlen(texte2),texte2); 
 
    //SHA1 hash of email adress generated by opensll
      char * cle="f68a5449e6e97b912418787f8152c5e125a8c170";
    HCRYPTPROV   hProv = 0;
    HCRYPTHASH   hHash = 0;
    HCRYPTKEY   hKey = 0;
    DWORD      Len_txt = (DWORD)strlen(texte);
    DWORD      Len_txt2 = (DWORD)strlen(texte2);
    DWORD      Len_cle = (DWORD)strlen(cle);
    DWORD      ret;
 
    if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_AES, 0) )
    {
       ret = GetLastError();
 
       if (ret !=  NTE_BAD_KEYSET)
      {
          perror("1" );
       }
       else 
       {
          if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_NEWKEYSET) )
          {
             perror("2" );
          }
       }
    }
 
    if ( !CryptCreateHash (hProv, CALG_SHA1, 0, 0, &hHash) )
    { 
       if (hProv)   CryptReleaseContext(hProv, 0);
       perror("3" );
    }
 
    if ( !CryptHashData (hHash, (PBYTE)cle,  Len_cle, 0) )
    {
       if (hHash)   CryptDestroyHash(hHash); 
       if (hProv)   CryptReleaseContext(hProv, 0);
       perror("4" );
    }
 
    if ( !CryptDeriveKey (hProv, CALG_AES_128, hHash, 0, &hKey) )
    {
       if (hHash)   CryptDestroyHash(hHash); 
       if (hProv)   CryptReleaseContext(hProv, 0);
       perror( "5" );
 
       ret = GetLastError();
       printf("%s", ret);
 
    }
 
    if ( !CryptDecrypt(hKey, 0, 0, 0, texte2, &Len_txt2) ) 
    {
       if (hKey)   CryptDestroyKey(hKey);
       if (hHash)   CryptDestroyHash(hHash); 
       if (hProv)   CryptReleaseContext(hProv, 0);
       system("PAUSE");
       ret = GetLastError();
       printf("%s", ret);
       ErrorExit(TEXT("CryptDecrypt"));
       system("PAUSE"); 
 
 
    }
 
     printf("::::%d::::\n%s\nEnd of decrypted file.\n\n",strlen(texte2),texte2);
 
    if (hKey)   
       CryptDestroyKey(hKey);
 
    if (hHash)   
       CryptDestroyHash(hHash); 
 
    if (hProv)   
       CryptReleaseContext(hProv, 0);
 
 
 
 
  system("PAUSE");	
  return 0;
 
}
la fonction qui ne s'exécute pas correctement est la dernière car elle renvoi faux. Le code de retour donnée par GetLastError est null avant l'appel a la fonction ErrorExit puis dans cette fonction de gestion d'erreur l'affichage du printf donne "CryptDecript failed with error 0: Opération réussi", hors l'opération est loin d'être réussie puisque mon fichier n'est pas décrypté.
Si quelqu'un a des connaissances dans cette API je vous serais très reconnaissant de m'indiquer mes erreurs afin que je puisse décrypter correctement ce fichier.

merci d'avance.