AES (aes-cbc-128, aes-cbc-192, aes-cbc-256) encryption/decryption with openssl C

Aug 9, 2013 at 6:26pm
I just want to test AES from openSSL with this 3 modes: with 128,192 and 256 key length but my decrypted text is different from my input and I dont know why. Also, when I pass a huge inputs length (lets say 1024 bytes) my program shows `core dumped`... My input is always the same but it doesnt matter, at least for now. Heres the code:

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
#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <openssl/aes.h>
    
    int main(int argc, char **argv)
    {
        int i;
        int keylength;
        printf("Give a key length [only 128 or 192 or 256!]:\n");
        scanf("%d", &keylength);
    
        /* generate a key with a given length */
        unsigned char aes_key[keylength];
        memset(aes_key, 0, sizeof(aes_key));
        if (!RAND_bytes(aes_key, keylength))
        {
            exit(-1);
        }
        aes_key[keylength-1] = '\0';
    
        int inputslength;
        printf("Give an input's length:\n");
        scanf("%d", &inputslength);
    
        /* generate input with a given length */
        unsigned char aes_input[inputslength+1];
        memset(aes_input, '0', sizeof(aes_input));
        aes_input[inputslength] = '\0';
    
        /*printf("original:\t");
        for(i=0; i<inputslength; i++)
        {
            printf("%c ", aes_input[i]);
        }
        printf("\n");*/
    
        /* init vector */
        unsigned char iv[AES_BLOCK_SIZE];
        if (!RAND_bytes(iv, AES_BLOCK_SIZE))
        {
            exit(-1);
        }
    
        //printf("AES_BLOCK_SIZE = %d\n", AES_BLOCK_SIZE); // aes block size is 16 bytes = 128 bits
        AES_KEY enc_key, dec_key;
        unsigned char enc_out[AES_BLOCK_SIZE];
        unsigned char dec_out[AES_BLOCK_SIZE];
    
        // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
        AES_set_encrypt_key(aes_key, keylength, &enc_key);
        AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT);
    
        AES_set_decrypt_key(aes_key, keylength, &dec_key);
        AES_decrypt(enc_out, dec_out, &dec_key);
    
        printf("original:\t");
        for(i=0;*(aes_input+i)!=0x00;i++)
            printf("%X ",*(aes_input+i));
        printf("\nencrypted:\t");
    
        for(i=0;*(enc_out+i)!=0x00;i++)
            printf("%X ",*(enc_out+i));
    
        printf("\ndecrypted:\t");
        for(i=0;*(dec_out+i)!=0x00;i++)
            printf("%X ",*(dec_out+i));
        printf("\n");
    
        /*printf("\n\noriginal:\t");
        for(i=0; i<inputslength; i++)
        {
            printf("%x ", dec_out[i]);
        }
        printf("\n");*/
    
    
        return 0;
    }


Aug 10, 2013 at 3:27pm
If you are using an initialization vector to encrypt wouldn't you need to use it to decrypt?
Topic archived. No new replies allowed.