Hello, this line seems wrong to me :
-> cbc_key = malloc(sizeof(argv[2])); argv[2] is a pointer... so y'ou're allocating the size of a pointer, which is 32 or 64 bits I didn't read the rest in detail, but it looks like you're making this error several times. try to use strlen instead of sizeof best regards ----- Mail d'origine ----- De: vineet59 <vineetsieta...@gmail.com> À: openssl-users@openssl.org Envoyé: Thu, 21 Aug 2014 21:20:31 +0200 (CEST) Objet: Generation of DES key for use in DES_encrypt1() #include <openssl/des.h> #include <string.h> #include <ctype.h> #define ENC 1 #define DEC 0 int isDecryption(char inputFile[]); void translate(unsigned char *dest, char *src); int allHex(char input[]); int main(int argc, char *argv[]) { if (argc != 5) { printf("Incorrect number of arguments\n"); return -1; } if (allHex(argv[1]) == 0) { printf("IV field contains non-hex characters\n"); return -1; } if (allHex(argv[2]) == 0) { printf("key field contains non-hex characters\n"); return -1; } FILE *fIn; FILE *fOut; fIn=fopen(argv[3],"r"); fOut=fopen(argv[4],"w"); if (fIn == NULL) { printf("Bad input file!\n"); return -1; } if (fOut == NULL) { printf("Bad output file!"); return -1; } int decrypt = isDecryption(argv[3]); int k; //int i = 0; DES_LONG block[2]; DES_LONG oldBlock[2]; DES_LONG tempBlock[2]; if (strlen(argv[2]) != 16) { printf("key is not the right length\n"); return -1; } unsigned char *cbc_key; cbc_key = malloc(sizeof(argv[2])); translate(cbc_key, argv[2]); if (strlen(argv[1]) != 16) { printf("iv is not the right length\n"); return -1; } unsigned char *temp; temp = malloc(sizeof(argv[1])); translate(temp, argv[1]); memcpy(oldBlock, temp, 8); DES_key_schedule key; //printf("%d-%s\n", strlen(cbc_key),cbc_key); //DES_cblock* if ((k = DES_set_key_checked((C_Block*)&cbc_key,&key)) != 0) printf("\nkey error\n%d\n",k); k = fread(block, 2, 32, fIn); //printf("%d",k); while (k == 2) { if (decrypt == 1) { DES_encrypt1(block, &key, DEC); } //XOR with the old block tempBlock[0] = block[0] ^ oldBlock[0]; tempBlock[1] = block[1] ^ oldBlock[1]; //encrypt block if (decrypt == 0) { DES_encrypt1(tempBlock, &key, ENC); } //oldblock = block memcpy(oldBlock, tempBlock, 8); //read block from file k = fread(&block, 1, sizeof(block), fIn); //write result to fOut fwrite(&tempBlock, 1, sizeof(tempBlock), fOut); } fclose(fIn); fclose(fOut); //printf("DES Clear Text: %ld%ld\n",in[0],in[1]); //des_encrypt1(in,key,ENC); //printf("DES Encryption: %u%u\n",in[0],in[1]); //des_encrypt1(in,key,DEC); } void translate(unsigned char *dest, char *src) { int i = 0; int j = 0; unsigned char t = 0; while(i < strlen(src)) { if (isdigit(src[i])) { t = t + src[i] - '0'; } else if (isupper(src[i])) { t = t + src[i] - 'A' + 10; } else if (islower(src[i])) { t = t + src[i] - 'a' + 10; } if (i % 2 == 0) { //multiply t by 16 t = t * 16; } else { // store in dest[j] dest[j] = t; t = 0; j++; } i++; } } //if file extension is .des returns true, other returns false int isDecryption(char inputFile[]) { int l = strlen(inputFile); if ((inputFile[l-1] == 's') && (inputFile[l-2] == 'e') && (inputFile[l-3] == 'd') && (inputFile[l-3] == '.')) { return 1; } else { return 0; } } //returns false if any chars are not 0-1 or a-f int allHex(char input[]) { int l = strlen(input)-1; int t = 0; while(l >= 0) { t = (int)input[l]; if ((t < ((int)'0')) || (t > ((int)'z'))) { return 0; } if ((t < ((int)'A')) && (t > ((int)'9'))) { return 0; } if ((t < ((int)'a')) && (t > ((int)'Z'))) { return 0; } l--; } return 1; } Cannot create key results in error to -1 and then segmentation fault on giving the following input: ./des_cbc1 fecdba9876543210 0123456789abcdef test.txt test.des Please respond ASAP!! Thanks -- View this message in context: http://openssl.6102.n7.nabble.com/Generation-of-DES-key-for-use-in-DES-encrypt1-tp52999.html Sent from the OpenSSL - User mailing list archive at Nabble.com. ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org