mcrypt - Problems with libmcrypt in C -


i have client server program in c encrypts/decrypts data mcrypt library. client encrypts string wants send server, send it, , after server reads, decrypts it. bellow encrypt , decrypt function:

encrypt function:

void encrypt(char *es, char *key, char *civ, size_t  length) {      mcrypt td;     int n;      td = mcrypt_module_open(mcrypt_twofish, null, mcrypt_cfb, null );     if (td == mcrypt_failed) {         log_err(log_opts, strerror(errno));         exit(1);     }     n = mcrypt_enc_get_iv_size(td);      char iv[n + 1];     strncpy(iv, civ, n);     iv[n] = '\0';      if ((mcrypt_generic_init(td, key, key_size, iv)) < 0) {         log_err(log_opts, "while trying mcrypt_generic_init.");         exit(1);     }     mcrypt_generic(td, es, length);      if (mcrypt_module_close(td) < 0) {         log_err(log_opts, "while trying close module.");         exit(1);     }  } 

decrypt function

void decrypt(char *ds, char *key, char *civ, size_t length) {     mcrypt td;     int n;      td = mcrypt_module_open(mcrypt_twofish, null, mcrypt_cfb, null );     n = mcrypt_enc_get_iv_size(td);      char iv[n + 1];     strncpy(iv, civ, n);     iv[n + 1] = '\0';      if ((mcrypt_generic_init(td, key, key_size, iv)) < 0) {         log_err(log_opts, "trying mcrypt_generic_init.");         exit(1);     }      mdecrypt_generic(td, ds, length);     if (mcrypt_module_close(td) < 0) {         log_err(log_opts, "while trying close module.");         exit(1);     }  } 

my problem:

there cases (1 10 rate) when string decrypted on server side encrypted on client side not same original. can suggest problem can come from?


edit

the response question, can find here, issue detailed further, being pointed out code contains bug.

in decrypt function:

char iv[n + 1]; strncpy(iv, civ, n); iv[n + 1] = '\0'; 

you have off-by-one overflow in iv[n + 1] = '\0'; statement.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -