Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

You are given a plaintext and a ciphertext, and you know that aes-128-cbc is used to generate the ciphertext from the plaintext, and you also know that the numbers in the IV are all zeros (not the...

1 answer below »
You are given a plaintext and a ciphertext, and you know that aes-128-cbc is used to generate the ciphertext from the plaintext, and you also know that the numbers in the IV are all zeros (not the ASCII character ‘0’). Another clue that you have learned is that the key used to encrypt this plaintext is an English word shorter than 16 characters; the word that can be found from a typical English dictionary. Since the word has less than 16 characters (i.e. 128 bits), space characters (hexadecimal value 0x20) are appended to the end of the word to form a key of 128 bits. Your goal is to write a program to find out this key. English word list is also provided along with the program structure in the Project-1.zip file. The plaintext and ciphertext is the following
Answered Same Day Oct 08, 2021

Solution

Shanaya answered on Oct 10 2021
145 Votes
New folde
encrypt.c
#include #include #include #include "openssl/evp.h"
#define MAX_LENGTH 16
A global variable t define max-length
int main(int argc, char* argv[])
{
    if (argc != 3 || strlen(argv[1]) != (MAX_LENGTH*4))
    {
        printf("\nUsage : %s HexaCipherSTring(Must be of %d length) WordFile\n", argv[0], (MAX_LENGTH*4));
        printf("\nUsage : %s 3EE6E497181171BD6980B51DB1043B71D93B2094518854BB70ADE3AC90DA68EC words-tnx5glxp.txt\n", argv[0]);
        return 0;
    }
    char *cipherTxt = argv[1];
    unsigned char cipherHexaTxt[MAX_LENGTH * 4];
    /* This variable is used to store the ciphertext provided to you. Make sure to use the proper format, eg. the hex 'd23a' would be written as {0xd2,0x3a} in C *
    unsigned char ciphertxt[] = { 0x7a, 0x6f, 0x6d, 0x62, 0x69, 0x65 };
zombie
    
This is the plaintext that we are encrypting. Do not change it as it may result in a different ciphertext
    unsigned char plaintext[] = "This is a top secret";
    
Pointer fp used to point to the English words file provided along.
    FILE *fp;
    
The initialization Vector is set to 0
    unsigned char iv[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    
Output buffer set to 1024 size
    unsigned char outbuf[1024];
    
Some other variables that are used in the program
    int outlen = 0, tmplen = 0;
    /* You may want to declare some additional variables to be used as flags*
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    bool isMatched;
    int lenOfCipherText;
    if (/* Use this statement to check is the words.txt file can be opened in 'r' permission */ !(fp = fopen(argv[2], "r")))
    {
        printf("\n E
or : Unable to open sample word file\n");
        /* Print a statement if the above condition is not met *
        return 0;
    }
    char key[MAX_LENGTH];
a
ay to store the key getting from the dictionary
    while (fgets(key, MAX_LENGTH, fp) != NULL)
    {
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            if (key[i] == '\n' || key[i] == 0)
when key is less than 16 characters, the extra left part is filled with " "
            {
                for (int j = i; j < strlen(key); j++)
                {
                    key[j] = ' ';
                }
                
eak;
            }
        }
        
Use the EVP li
ary to initialize ciphe
        EVP_CIPHER_CTX_init(ctx);
        
Use the EVP li
ary to encrypt the ciphe
        EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, (unsigned char*)key, iv);
        
Checking to see if EVP_EncryptUpdate is valid or not
        if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, plaintext, strlen((char*)plaintext)))
        {
            printf("\nE
or : invalid encryption\n");
            /*Print Out a relevant E
or Messege*
            return 0;
        }
        
Buffer passed to EVP_EncryptFinal() must be after data just encrypted to avoid overwriting it
        
Checking to see if !EVP_EncryptFinal_ex is valid or not
        if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
        {
            printf("\nE
or : Not a valid encryption\n");
            /*Print Out a relevant E
or Messege*
            return 0;
        }
        outlen += tmplen;
        EVP_CIPHER_CTX_cleanup(ctx);
        
print cu
et key and its co
esponding ciphertext
        printf("The key is : %s The Co
esponding Cipher Text Is: ", key);
        char *hex_str = (char*) malloc((outlen * 2) + 1);
        int i = 0;
        /* Use a loop to print out the buffer *
        for (int j = 0; j < outlen; j++)
        {
            
String char to hexa cha
            sprintf(&hex_str[j * 2], "%02x", outbuf[j]);
        }
        for (int q = 0; q < (outlen * 2) + 1; q++)
        {
            printf("%c", hex_str[q]);
        }
        printf("\n");
        isMatched = true;
        for (int q = 0; q < (outlen * 2) + 1 ; q++)
        {
            if (cipherTxt[q] != hex_str[q])
            {
                isMatched = false;
                
eak;
            }
            /* Judge whether the cipher text generated by this key is match for the provoded one *
            /* As the whole ciphertext cannot be matched at once, use this loop to match it bit by bit *
        }
        if (isMatched /* If the generated ciphertext matched with the one provided*/)
        {
            printf("\n*****************************************************\n");
            /* Print the key used *
            printf("\nKey Used %s \n", key);
            /* Print the text in the output buffer *
            printf("\nOutput buffer ");
            for (int q = 0; q < (outlen * 2) + 1; q++)
            {
                printf("%c", hex_str[q]);
            }
            printf("\nProvided Cipher text %s", cipherTxt);
            /* Print the length of the ciphertext *
            printf("\nLength of cipher text %d \n", MAX_LENGTH*4);
            printf("\n*****************************************************\n");
            return 0;
        }
    }
    fclose(fp);
    return 0;
}
New folde
HowTo.txt
Usage : MyRepo.exe HexaCipherSTring(Must be of 64 length) WordFile
Usage : MyRepo.exe 3EE6E497181171BD6980B51DB1043B71D93B2094518854BB70ADE3AC90DA68EC words-tnx5glxp.txt
===================================================================================================
gcc solution :
Install gcc(https:
preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/)
Install OpenSSL(https:
tecadmin.net/install-openssl-on-windows/)
un command :
gcc -o enc encrypt.c -std=c99 -lcrypto -ldl
Note : You may faced compilation and Linker e
ors, if Open ssl is not install co
ectly
Note : for gcc on windows you need to run
gcc -o enc encrypt.c -std=c99 -lcrypto -ldl -luitest.lib -lpadlock.lib -lossltest.lib -lopenssl.lib -llibtestutil.lib -llibssl_static.lib -llibssl.lib -llibcrypto_static.lib -llibcrypto.lib -llibapps.lib -ldasync.lib -lcapi.lib -lWs2_32.lib -lruntimeobject.lib -I====================================================================================================
For VC++ :
Unzip MyRepo.zip
Open the solution in VS 2017 and rebuild
New...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here