Bonjour,

J'ai écris une fonction qui fait chiffrement/déchiffrement de fichiers.
A la fin d'une opération, je supprime le fichier original avec la fonction remove() (celui en clair en cas de chiffrement ou celui chiffré en cas de déchiffrement).

Sur Linux (ubuntu) ça marche super bien.
Sur windows (seven), j'ai remarqué que pendant le déchiffrement, le fichier chiffré ne peut pas être supprimé ! la fonction remove retourne -1. Et quand j'essaye de le supprimer à la main pendant l'exécution du programme -pour voir- on me dit que le fichier est utilisé ! alors que je fais bien fclose() à la fin de chaque opération !

Voici ma fonction :

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
int do_crypt (const char *infile, const char *key_s, const char *iv_key_s, const char *iv_file_s, const char *fileId_s, int do_encrypt){
 
    /* Allow enough space in output buffer for additional block */
    unsigned char inbuf[SIZE_BUF], outbuf[SIZE_BUF + EVP_MAX_BLOCK_LENGTH],
                  key[16], iv[16];
 
    char outfile[255]="";
 
    int inlen = 0, outlen = 0;
 
    FILE *in = fopen(infile,"rb"),
         *out;
 
    if(in==NULL) {
        //printf("\ndo_crypt error : file %s not found !",infile);
        printTime(var_time);
        flog = fopen(LOG_FILE_NAME,"a");
        fprintf(flog,"%s\ndo_crypt error : file %s not found !\n",var_time,infile);
        fclose(flog);
        return 0;
    }
 
    if(do_encrypt){ //if encryption case
        char iv_fileid_s[40*2+1]="";
        unsigned char iv_fileid[40];
        strcat(iv_fileid_s,iv_key_s);
        strcat(iv_fileid_s,iv_file_s);
        strcat(iv_fileid_s,fileId_s);
        stringToBytes(iv_fileid_s,iv_fileid);
        strcat(outfile,infile);
        strcat(outfile,".aes");
        out = fopen(outfile,"wb");
        if(out==NULL) {
            //printf("\ndo_crypt() : an error occured when writing to the file %s !",outfile);
            printTime(var_time);
            flog = fopen(LOG_FILE_NAME,"a");
            fprintf(flog,"%s\ndo_crypt() : an error occured when writing to the file %s !\n",var_time,outfile);
            fclose(flog);
            return 0;
        }
        fwrite(iv_fileid, 1, 40, out);
    }
    else{ //if decryption case
        strncpy(outfile,infile,strlen(infile)-4);
        out = fopen(outfile,"wb");
        if(out==NULL) {
            //printf("\ndo_crypt() : an error occured when writing to the file %s !",outfile);
            printTime(var_time);
            flog = fopen(LOG_FILE_NAME,"a");
            fprintf(flog,"%s\ndo_crypt() : an error occured when writing to the file %s !\n",var_time,outfile);
            fclose(flog);
            return 0;
        }
        inlen = fread(inbuf, 1, 40, in); //Read IVs+FILE_ID = header (to pass directly to encrypted data)
    }
 
    stringToBytes(key_s,key);
    stringToBytes(iv_file_s,iv);
 
    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv, do_encrypt);
    for(;;)
    {
        inlen = fread(inbuf, 1, SIZE_BUF, in);
        if(inlen <= 0) break;
        if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
        {
            /* Error */
            EVP_CIPHER_CTX_cleanup(&ctx);
            fclose(in);
            fclose(out);
            remove(outfile);
            //printf("\ndo_crypt() Error : in EVP_CipherUpdate()");
            printTime(var_time);
            flog = fopen(LOG_FILE_NAME,"a");
            fprintf(flog,"%s\ndo_crypt() Error : in EVP_CipherUpdate()\n",var_time);
            fclose(flog);
            return 0;
        }
        fwrite(outbuf, 1, outlen, out);
    }
 
    if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
    {
        /* Error */
        EVP_CIPHER_CTX_cleanup(&ctx);
        fclose(in);
        fclose(out);
        remove(outfile);
        //printf("\ndo_crypt() Error : in EVP_CipherFinal_ex()");
        printTime(var_time);
        flog = fopen(LOG_FILE_NAME,"a");
        fprintf(flog,"%s\ndo_crypt() Error : in EVP_CipherFinal_ex()\n",var_time);
        fclose(flog);
        return 0;
    }
 
    fwrite(outbuf, 1, outlen, out);
    EVP_CIPHER_CTX_cleanup(&ctx);
 
    fclose(in);
    fclose(out);
 
    remove(infile);
 
    return 1;
}
Merci,
Shano.