I have a possible password list which one of them is used to encrypt "This is a top secret.". The words that are less than 16 bytes need to be padded with # sign. but somehow I am unable to produce the correct cipher text. Here the known information for this question:
Ciphertext in hex format:
764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2
IV in hex format:
aabbccddeeff00998877665544332211
I know that passphrase is "Syracuse########" and I tried openssl commands on linux to decrypt it.
my code gets this word list and then creates matchingresult.txt which contains all the ciphertext for possible passkeys. I don't know where do I make a mistake that none of these ciphertexts are true.Used wordlist
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <openssl/evp.h>
void pad(char *s, int length);
int print_result(unsigned char *buf, char *s, int len, FILE *outFile, char *match);
int strcicmp(char const *a, char const *b);
int main(){
unsigned char match[] = "MATCH";
unsigned char noMATCH[] = "NO MATCH";
int i;
char words[16],t; // each word in dictionary
FILE *key, *outFile;
unsigned char outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
unsigned char iv[] = {0xaa,0xbb, 0xcc, 0xdd,0xee, 0xff, 0x00, 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11};// given in the description of task 5
int outlen, tmplen;
int num;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
char inText[] = "This is a top secret."; //given in the decription of task 5
char cipherTextGiven[] = "764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2"; //given in the description of task 5
key = fopen("words.txt", "r"); //provided file by SEED Labs
if(remove("matchingResult.txt") == -1){ // the file contains result of each key
perror("Error deleting file");
}
outFile = fopen("matchingResult.txt","a+");
if(key<0 || outFile < 0){
perror("Cannot open file");
exit(1);
}
char pbuffer[1024];
while ( fgets(words,16,key)) // get each word from dictionary that suppose to be a key to encrypt
{
i=strlen(words);
words[i-1]='\0'; // in the text editor it automatically adds null or end of file so we need to remove that
i=strlen(words);
if (i < 16){ // 16 because we use AES-128
// since the word has less than 16 characters (i.e. 128 bits), pound signs (#: hexadecimal value is 0x23)
// are appended to the end of the word to form a key of 128 bits
pad(words, (16));
}
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;
int i;
char* buf_str = (char*) malloc (2*outlen + 1);
char* buf_ptr = buf_str;
for (i = 0; i<outlen;i++){
buf_ptr += sprintf(buf_ptr, "%02X" , outbuf[i]);
}
*(buf_ptr + 1) = '\0';
if (strcicmp(cipherTextGiven, buf_str) == 0)
print_result(outbuf, words, outlen, outFile, match);
else
print_result(outbuf, words, outlen, outFile, noMATCH);
}
fclose(key);
fclose(outFile);
return 1;
}
// print result to output file matchresult.txt
int print_result(unsigned char *buf, char *s, int len, FILE *outFile, char *match){
int i,n,j,k;
char x='\n';
char space = ' ';
for ( j=0; j<strlen(s); j++){
fprintf(outFile,"%c",s[j]);
}
fprintf(outFile,"%c",space);
for ( i = 0 ; i<len; i++){
fprintf(outFile,"%02x", buf[i]);
}
fprintf(outFile, "%c", space);
for (k=0; k< strlen(match); k++){
fprintf(outFile,"%c", match[k]);
}
fprintf(outFile,"%c",x);
return(0);
}
// add padding to the key
void pad(char *s, int length){
int l;
l= strlen(s); // its length
while(l<length){
s[l] = '#'; // insert a pound sign
l++;
}
s[l] = '\0'; // strings need to be terminated in a null
}
// compare case insensitive
int strcicmp(char const *a, char const *b){
for(;;a++,b++){
int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a)
return d;
}
}
problem was caused by this part of the code: s[l] = '\0';