Hi, I am using aes-128-cbc encryption using openssl. I have to encrypt the plain text using the words I have in the file which act as key. I have a cipher text. I have to compare the encrypted text with the cipher text already declared in the program. The following is the program I have written. My plain text is 21 bytes and key is sometimes <16 or > 16 bytes in length because the word list is a dictionary of words. When I run my code I am getting different cipher texts every single time. I am not sure why this is happening. Is there any thing that is going wrong in my code.
The following is my code. Please let me know where I am making a mistake. #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/evp.h> int main() { int i; char words[32], t; FILE *key, *outFile; const char *out = "Output.txt"; unsigned char outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; unsigned char iv[] = "0000000000000000"; int outlen, tmplen; int num; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); char inText[] = "This is a top secret."; char cipherText[] = "8d20e5056a8d24d0462ce74e4904c1b513e10d1df4a2ef2ad4540fae1ca0aaf9"; key = fopen("words.txt", "r"); if( remove("ciphertext.txt") == -1 ) { perror("Error deleting file"); } outFile = fopen("ciphertext.txt", "a+"); if( key < 0 || outFile < 0 ) { perror ("Cannot open file"); exit(1); } char pbuffer[1024]; while ( fgets(words,32, key) ) { i=strlen(words); words[i-1]='\0'; //printf("%s",words); i = 0; EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, words, iv); if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, inText, strlen(inText))) { EVP_CIPHER_CTX_cleanup(&ctx); return 0; } if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) { EVP_CIPHER_CTX_cleanup(&ctx); return 0; } outlen += tmplen; print_hex(outbuf, outlen, outFile); } fclose(key); fclose(outFile); return 1; } int print_hex(unsigned char *buf, int len, FILE *outFile) { int i,n; char x='\n'; for ( i = 0; i < len; i++ ) { fprintf(outFile,"%02x",buf[i]); } fprintf(outFile,"%c",x); return(0); } Harish K Bayyavarapu Master's Computer Science University of Texas at El Paso Phone: 915-929-0999