Hi,
I have a plain text and I have the cipher text with me and my task is to find the key for the cipher text declared. The key is a word list like a dictionary. I have written the code in c and it compiles perfect and creates the file with all the ciphers. The problem I am facing is that every time i run the code a cipher text is completely different. I have no clue where I am making a mistake. The following is the code I had written
#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); 
}
Since the key is a word. The words in the wordlist can be of size < or > 16 bytes and from my research on openssl it was said that there will be a pkcs#5 padding if the block length is does not fit into 16bytes. Is it the same case for the key also.
The cipher text I declared does not match with the cipher text I am generating from the program and I am unable to find the key for the cipher text.
I need help from the experts. I would appreciate if some one helps me in getting out of the trouble
Thanks in advance