IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Vecteur d'initialisation avec mcrypt


Sujet :

C

  1. #1
    Membre à l'essai
    Inscrit en
    Décembre 2006
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 24
    Points : 22
    Points
    22
    Par défaut Vecteur d'initialisation avec mcrypt
    Bonsoir,

    j'ai un problème d'initialisation de mon vecteur d'initialisation () utilisé avec mcrypt. En gros, dès que j'utilise une fonction de la famille des rand, le résultat du décryptage est aléatoire: soit ça marche, soit c'est tronqué, soit ça affiche n'importe quoi. Par contre, avec un truc "simple", du style:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    for (i = 0; i <.....)
       IV[i] = (255 % i ) +13;
    la ça marche nickel.
    L'IV est de type char* mais la fonction de cryptage/décryptage accepte du void *, et curieusement si jlui passe du int* ça plante...

    Voilà, si quelqu'un voit une solution, je lui en serait reconnaissant

    Merci.

    P.S: si vous voulez le code, y'a pas de problèmes...

  2. #2
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 370
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 370
    Points : 23 625
    Points
    23 625
    Par défaut
    Citation Envoyé par snacksou Voir le message
    P.S: si vous voulez le code, y'a pas de problèmes...
    Ce serait plus facile avec, en effet.

  3. #3
    Membre à l'essai
    Inscrit en
    Décembre 2006
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 24
    Points : 22
    Points
    22
    Par défaut
    Je voulais pas le mettre car il est pas mal long, mais c'est vrai que ça aide à comprendre :p

    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
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
     
    #include "secure.h"
    #include "base64.h"
    #include <stdio.h>
    #define HASH_MODE MHASH_SHA1
     
    char *
    my_hash (char *data)
    {
       MHASH td;
       unsigned char *hash =
          malloc (mhash_get_block_size (MHASH_SHA1) * sizeof (char) + 1);
       memset (hash, '\0', mhash_get_block_size (MHASH_SHA1));
       td = mhash_init (HASH_MODE);
       mhash (td, data, (int) strlen (data));
       mhash_deinit (td, hash);
     
       char *hashstr =
          malloc ((mhash_get_block_size (MHASH_SHA1) * sizeof (char) * 2) + 1);
       memset (hashstr, '\0', mhash_get_block_size (MHASH_SHA1));
       int i;
       char tmp[2];
       sprintf (tmp, "%.2x", hash[0]);
       strcpy (hashstr, tmp);
       for (i = 1; i < mhash_get_block_size (MHASH_SHA1); ++i)
       {
          sprintf (tmp, "%.2x", hash[i]);
          strcat (hashstr, tmp);
       }
       free (hash);
       return hashstr;
    }
     
    char *
    my_encrypt (const char *data, const char *password)
    {
    #ifndef PLAINTTEXT
    #ifdef DEBUG
       printf
          ("--------------------------------------- Module de Cryptage ----------------------------------------\n");
    #endif
     
       MCRYPT td;
       int ivsize, keysize, datasize, passsize;
       int i;
       char *IV;
       char *key;
       char *encrypt;
       char *pass;
     
       char *algo = "rijndael-128";
       char *mode = "cfb";
       if (mcrypt_module_self_test (algo, NULL) != 0)
       {
          fprintf (stderr, "Did not pass the test\n");
          exit (1);
       }
     
       td = mcrypt_module_open (algo, NULL, mode, NULL);
     
       if (td != MCRYPT_FAILED)
       {
          // Key initialisation
          passsize = strlen (password);
          pass = calloc (1, passsize);
          memset (pass, '\0', passsize);
          memmove (pass, password, passsize);
          keysize = mcrypt_enc_get_key_size (td);
          key = calloc (1, keysize);
          if (mhash_keygen(KEYGEN_MCRYPT, MHASH_SHA1, 0, key, keysize, NULL, 0,(unsigned char *) pass, passsize) != 0)
          {
    	 fprintf (stderr, "Couldn't get hash from key\n");
    	 exit (1);
          }
          // IV initialisation
          srand (time (0));
          ivsize = mcrypt_enc_get_iv_size (td);
     
          IV = calloc (1, ivsize + 1);
          memset (IV, '\0', ivsize + 1);
          for (i = 0; i < ivsize; ++i)
          {
    	 //IV[i] = rand ();
    	 IV[i] = 1;
          }
     
          // initialisation td
          if (mcrypt_generic_init (td, key, keysize, IV) < 0)
          {
    	 fprintf (stderr, "Couldn't initialize algorithm\n");
    	 exit (1);
          }
     
          datasize = strlen (data);
          encrypt = malloc (sizeof (char) * datasize + 1);
          memset (encrypt, '\0', datasize + 1);
          memmove (encrypt, data, datasize);
    #ifdef DEBUG
          printf ("\t Avant Cryptage : %s\n", encrypt);
    #endif
          // encryption
          if (mcrypt_generic (td, encrypt, datasize) != 0)
          {
    	 fprintf (stderr, "Couldn't encrypt\n");
    	 exit (1);
          }
       }
       else
       {
          fprintf (stderr, "Couldn't open module %s\n", algo);
          exit (1);
       }
    #ifdef DEBUG
       printf ("\t Apres Encrypt : %s\n", encrypt);
       printf ("\t IV : %s\n", IV);
    #endif
     
       // On encode le message crypté et l'IV séparé par :
       size_t len;
       char *base64encrypt =(char *) base64_encode ((unsigned char *) encrypt, strlen (encrypt),&len);
       char *base64IV =(char *) base64_encode ((unsigned char *) IV, strlen (IV), &len);
       int size_result = strlen (base64encrypt) + strlen (base64IV) + 2;
       char *result = malloc (sizeof (char) * size_result);
    #ifdef DEBUG
       printf ("\t Base64 Encrypt : %s\n", base64encrypt);
       printf ("\t Base64 IV : %s\n", base64IV);
    #endif
     
       memset (result, '\0', size_result);
       sprintf (result, "%s:%s", base64encrypt, base64IV);
     
       // free
       mcrypt_generic_deinit (td);
       mcrypt_module_close (td);
       free (key);
       free (pass);
       free (IV);
       free (encrypt);
       free (base64encrypt);
       free (base64IV);
    #ifdef DEBUG
       printf
          ("---------------------------------------- Fin de Cryptage ----------------------------------------\n");
    #endif
       return result;
    #else
       char *tmp = malloc (sizeof (char) * strlen (data) + 1);
       memset (tmp, '\0', strlen (data) + 1);
       tmp = strcpy (tmp, data);
       return tmp;
    #endif
    }
     
     
    char *
    my_decrypt (const char *data, const char *password)
    {
    #ifndef PLAINTTEXT
     
    #ifdef DEBUG
       printf
          ("-------------------------------------- Module de Decryptage ---------------------------------------\n");
    #endif
       MCRYPT td;
       int keysize, datasize, passsize;
       char *key;
       char *decrypt;
       char *pass;
       char *IV;
       char *encrypt;
       size_t size_encrypt, len;
       char *tmp = malloc (sizeof (char) * strlen (data) + 1);
       memset (tmp, '\0', strlen (data) + 1);
       tmp = strcpy (tmp, data);
     
       char *base64encrypt = strtok (tmp, ":");
       char *base64IV = strtok (NULL, "\0");
     
    #ifdef DEBUG
       printf ("\t Base64 Encrypt : %s\n", base64encrypt);
       printf ("\t Base64 IV : %s\n", base64IV);
    #endif
     
       encrypt =(char *) base64_decode ((unsigned char *) base64encrypt,
    					  strlen (base64encrypt), &size_encrypt);
       IV =(char *) base64_decode ((unsigned char *) base64IV, strlen (base64IV),
    			       &len);
    #ifdef DEBUG
       printf ("\t Encrypt : %s\n", encrypt);
       printf ("\t IV : %s\n", IV);
    #endif
     
       char *algo = "rijndael-128";
       char *mode = "cfb";
     
       if (mcrypt_module_self_test (algo, NULL) != 0)
       {
          fprintf (stderr, "Did not pass the test\n");
          exit (1);
       }
     
       td = mcrypt_module_open (algo, NULL, mode, NULL);
     
       if (td != MCRYPT_FAILED)
       {
          /*
          // recup du pointeur sur le ":" en partant de la fin de data et à partir d'au moins la taille de l'iv (au cas ou l'IV contienne des ":")
          char *base64IV = memrchr (data, ':', strlen(data)-mcrypt_enc_get_iv_size(td));
          // pointeur +1 pour pas avoir le ":"
          base64IV = base64IV+1;
          
          int encryptsize = strlen(data)-strlen(base64IV);
          char *base64encrypt = malloc (encryptsize);
          strncpy (base64encrypt, data, encryptsize);
          
          #ifdef DEBUG
          printf ("\t Base64 Encrypt : %s\n", base64encrypt);
          printf ("\t Base64 IV : %s\n", base64IV);
          #endif
          
          char *encrypt =(char *) base64_decode ((unsigned char *) base64encrypt,
          strlen (base64encrypt), &size_encrypt);
          IV =(char *) base64_decode ((unsigned char *) base64IV, strlen (base64IV),
          &len);
          #ifdef DEBUG
          printf ("\t Encrypt : %s\n", encrypt);
          printf ("\t IV : %s\n", IV);
          #endif
          */
     
          // Key initialisation
          passsize = strlen (password);
          pass = calloc (1, passsize);
          memset (pass, '\0', passsize);
          memmove (pass, password, passsize);
          keysize = mcrypt_enc_get_key_size (td);
          key = calloc (1, keysize);
          if (mhash_keygen(KEYGEN_MCRYPT, MHASH_SHA1, 0, key, keysize, NULL, 0,(unsigned char *) pass, passsize) != 0)
          {
    	 fprintf (stderr, "Couldn't get hash from key\n");
    	 exit (1);
          }
     
          // initialisation
          if (mcrypt_generic_init (td, key, keysize, IV) < 0)
          {
    	 fprintf (stderr, "Couldn't initialize algorithm\n");
    	 exit (1);
          }
     
          // data copy
          datasize = size_encrypt;
          decrypt = calloc (1, datasize + 1);
          memset (decrypt, '\0', datasize + 1);
          memmove (decrypt, encrypt, datasize);
    #ifdef DEBUG
          printf ("\t Avant Decryptage : %s\n", decrypt);
    #endif
          // decryption
          if (mdecrypt_generic (td, decrypt, datasize) != 0)
          {
    	 fprintf (stderr, "Couldn't encrypt\n");
    	 exit (1);
          }
       }
       else
       {
          fprintf (stderr, "Couldn't open module %s\n", algo);
          exit (1);
       }
    #ifdef DEBUG
       printf ("\t Après Decryptage : %s\n", decrypt);
    #endif
       //free
       mcrypt_generic_deinit (td);
       mcrypt_module_close (td);
       free (key);
       free (pass);
       //free (tmp);
       free (IV);
       free (encrypt);
    #ifdef DEBUG
       printf
          ("---------------------------------------- Fin de Decryptage ----------------------------------------\n");
    #endif
       return decrypt;
    #else
       char *tmp = malloc (sizeof (char) * strlen (data) + 1);
       tmp = strcpy (tmp, data);
       return tmp;
    #endif
    }
    y'a deux versions. Afin de modifier:
    * commenter les lignes 175 à 190 comprise
    * changer le "IV[i] = 1" en "IV[i] = rand()" ligne 82-83
    * décommenter les lignes 206 à 227

    dans les deux cas, on a des problèmes: des fois le hash est tronqué, et des fois ça plante tout simplement.

    Merci d'avance


    Edit: arf, jviens de me rendre compte que y'a pas les lignes sur le post. Il se trouve ici sinon.

Discussions similaires

  1. Problème d'initialisation avec Hibernate
    Par fabou3377 dans le forum NetBeans
    Réponses: 3
    Dernier message: 03/04/2009, 06h41
  2. [mcrypt] Vecteur d'initialisation
    Par sabotage dans le forum Langage
    Réponses: 3
    Dernier message: 31/03/2009, 10h30
  3. [MCRYPT] cryptage/décryptage avec mcrypt
    Par Zobbiwan dans le forum Langage
    Réponses: 7
    Dernier message: 10/08/2007, 09h52
  4. [TListview] Initialisation avec un fichier texte
    Par Lenaick dans le forum C++Builder
    Réponses: 1
    Dernier message: 14/03/2006, 18h44
  5. Problème d'initialisation avec GLFW
    Par adrien357 dans le forum Composants VCL
    Réponses: 4
    Dernier message: 23/10/2005, 18h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo