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

Linux Discussion :

mcrypt et IV


Sujet :

Linux

  1. #1
    Membre régulier
    Inscrit en
    Décembre 2005
    Messages
    109
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Décembre 2005
    Messages : 109
    Points : 92
    Points
    92
    Par défaut mcrypt et IV
    Salut à tous,

    j'ai trouvé ce code https://gist.github.com/bricef/2436364/ qui crypte et décrypte d'un coup.
    j'ai commencé à le modifier pour crypter en vue de stocker ce qui est chiffré puis le décrypter plus tard mais ça veut pas.

    faut-il stocker l'IV qque part pour décrypter plus tard avec le même IV ? je débute avec la crypto, je connais pas grand chose.
    je colle mon code modifié, dans un 1er choix je crypte j'affiche l'IV puis au choix suivant je fais un C/C du texte chiffré et de l'IV mais ça me réaffiche le texte chiffré.

    Code C : 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
     
    int encrypt(
        void* buffer,
        int buffer_len, /* Because the plaintext could include null bytes*/
        char* IV,
        char* key,
        int key_len
    ){
     
        int i = 0;
     
        MCRYPT td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
        int blocksize = mcrypt_enc_get_block_size(td);
        if( buffer_len % blocksize != 0 ){
            return 1;
        }
     
        mcrypt_generic_init(td, key, key_len, IV);
        mcrypt_generic(td, buffer, buffer_len);
        mcrypt_generic_deinit (td);
        mcrypt_module_close(td);
     
        return 0;
    }
     
    int decrypt(
        void* buffer,
        int buffer_len,
        char* IV,
        char* key,
        int key_len
    ){
        MCRYPT td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
        int blocksize = mcrypt_enc_get_block_size(td);
        if( buffer_len % blocksize != 0 ){
            return 1;
        }
     
        mcrypt_generic_init(td, key, key_len, IV);
        mdecrypt_generic(td, buffer, buffer_len);
        mcrypt_generic_deinit (td);
        mcrypt_module_close(td);
     
        return 0;
    }
     
    void display(char* ciphertext, int len){
        int v;
     
        for (v=0; v<len; v++){
            printf("%d ", ciphertext[v]);
        }
        printf("-\n");
    }
     
    int main()
    {
     
        char plaintext[SIZE + 1] = {0};
        char key[SIZE_PWD + 1] = {0};
        int rtn = 0;
     
        MCRYPT td, td2;
     
        char* IV;
     
        int keysize = 16; /* 128 bits */
        char* buffer;
        int buffer_len = 0;
        int i = 0;
     
        int choix = 0;
     
        td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
        IV = malloc(mcrypt_enc_get_iv_size(td));
     
        for (i=0; i< mcrypt_enc_get_iv_size( td); i++) {
            IV[i]=rand();
        }
     
     
        printf("Saisir le pwd :\n");
        do
        {
            rtn = scanf(" %"XSTR(SIZE_PWD)"[^\n]", key);
            clean_stdin();
        }
        while (rtn != 1);
        keysize = strlen(key);
     
        printf("Saisir le texte :\n");
        rtn = 0;
        do
        {
            rtn = scanf(" %"XSTR(SIZE)"[^\n]", plaintext);
            clean_stdin();
        }
        while (rtn != 1);
     
        buffer_len = strlen(plaintext);
        buffer = calloc(1, buffer_len);
        strncpy(buffer, plaintext, buffer_len);
     
        do {
            printf("\nSaisir le choix (0 crypter, 1 décrypter, 2 quitter) :\n");
            scanf("%d", &choix);
     
            if(choix == 0 ){
     
                printf("plain:   %s\n", plaintext);
                encrypt(buffer, buffer_len, IV, key, keysize);
                printf("cipher:  ");
                display(buffer , buffer_len);
                printf("IV :%x-", IV);
            }
            else if( choix == 1 ){
     
                printf("Saisir le pwd :\n");
                do
                {
                    rtn = scanf(" %"XSTR(SIZE_PWD)"[^\n]", key);
                    clean_stdin();
                }
                while (rtn != 1);
                keysize = strlen(key);
     
                printf("Saisir le texte :\n");
                rtn = 0;
                do
                {
                    rtn = scanf(" %"XSTR(SIZE)"[^\n]", plaintext);
                    clean_stdin();
                }
                while (rtn != 1);
     
                buffer_len = strlen(plaintext);
                buffer = calloc(1, buffer_len);
                strncpy(buffer, plaintext, buffer_len);
     
                fflush(stdin);
     
                printf("Saisir l'IV :\n");
                rtn = 0;
                do
                {
                    rtn = scanf(" %"XSTR(SIZE)"[^\n]", IV);
                    clean_stdin();
                }
                while (rtn != 1);
                display(buffer , buffer_len);
     
                decrypt(buffer, buffer_len, IV, key, keysize);
                printf("decrypt: %s\n", buffer);
            }
     
        } while( choix != 2);
     
        free(IV);
        free(buffer);
     
        return 0;
    }

    Merci

    Edit : ajout des fonctions annexes.
    “La folie, c’est se comporter de la même manière et s’attendre à un résultat différent.” Albert E.

  2. #2
    Membre régulier
    Inscrit en
    Décembre 2005
    Messages
    109
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Décembre 2005
    Messages : 109
    Points : 92
    Points
    92
    Par défaut
    j'ai trouvé
    pour le décryptage il faut lire une suite d'entiers sans les espaces, j'ai donc modifié ma saisie tu texte chiffré comme suit, la fin de la saisie est détectée par le -1

    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
     
            else if( choix == 1 ){
     
                printf("Saisir le pwd :\n");
                do
                {
                    rtn = scanf(" %"XSTR(SIZE_PWD)"[^\n]", key);
                    clean_stdin();
                }
                while (rtn != 1);
                keysize = strlen(key);
     
                printf("Saisir le texte :\n");
                rtn = 0;
                do
                {
                    scanf("%d", &plaintext[rtn]);
                    if(plaintext[rtn] == -1) break;
                    clean_stdin();
                    rtn++;
                }
                while (rtn != 15);
                plaintext[rtn] = '\0';
     
                buffer_len = strlen(plaintext);
                buffer = calloc(1, buffer_len);
                strncpy(buffer, plaintext, buffer_len);
     
                display(buffer , buffer_len);
     
                decrypt(buffer, buffer_len, IV, key, keysize);
                printf("decrypt: %s\n", buffer);
            }
    Have fun
    “La folie, c’est se comporter de la même manière et s’attendre à un résultat différent.” Albert E.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 5
    Dernier message: 16/06/2010, 04h34
  2. [MCRYPT] Fonction mcrypt erreur
    Par zulot dans le forum Langage
    Réponses: 3
    Dernier message: 17/07/2007, 12h57
  3. [PHP-JS] Chiffrement réversible sans mcrypt
    Par hisy dans le forum Langage
    Réponses: 6
    Dernier message: 27/03/2007, 15h50
  4. [MCRYPT] Est-il possible de crypter des fichiers avec les bibliothèques de hash ?
    Par a028762 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 01/12/2006, 09h18

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