0

I have to encrypt some data using a 4 byte IV. However, the encryption algorithm that I am using (AES128)needs a 16 byte (128 bit) key. Say, my code is as follows:

#include<gcrypt.h>
void encrypt(){
    int IV = 6174;
    gcry_cipher_hd_t hd;        
    errStatus = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
    errStatus = gcry_cipher_setkey(hd, keyBuffer, 16);
    gcry_cipher_setiv(hd, &IV, 16);
    gcry_cipher_encrypt(hd, output, 16, bytesToEncrypt, 16);
}

Say keyBuffer contains a 16 byte key and bytes,output` are my input and output respectively. How do I go about properly giving the IV?

TheRookierLearner
  • 3,643
  • 8
  • 35
  • 53
  • 2
    This seems like the wrong thing to do. A small IV will compromise encryption security. And if you are using a constant IV like in your example, it will be greatly compromised. The IV is supposed to be random and different for each message that is encrypted. – interjay Jan 24 '14 at 00:40
  • I know. Its just a class exercise to get familiar with crypto libraries. – TheRookierLearner Jan 24 '14 at 00:45

1 Answers1

0

Can you try this code just for avoiding buffer overflow? I haven't tested this myself, though

int IV[4] = { 6174 }; // { 6174, 0, 0, 0 }
nodakai
  • 7,773
  • 3
  • 30
  • 60
  • "Segmentation fault (core dumped)" :-( – TheRookierLearner Jan 24 '14 at 01:36
  • `gcc -O0 -g` (oh-zero gee) will produce a binary suitable for debugging. Can you run the debug binary under `gdb` to see exactly where the SEGV happened? And you can type `bt` to show the stack frames. http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html – nodakai Jan 24 '14 at 01:46