Answer To: You are given a plaintext and a ciphertext, and you know that aes-128-cbc is used to generate the...
Shanaya answered on Oct 10 2021
New folder/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 Error : Unable to open sample word file\n");
/* Print a statement if the above condition is not met */
return 0;
}
char key[MAX_LENGTH]; //array 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] = ' ';
}
break;
}
}
// Use the EVP library to initialize cipher
EVP_CIPHER_CTX_init(ctx);
// Use the EVP library to encrypt the cipher
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("\nError : invalid encryption\n");
/*Print Out a relevant Error 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("\nError : Not a valid encryption\n");
/*Print Out a relevant Error Messege*/
return 0;
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(ctx);
//print curret key and its corresponding ciphertext
printf("The key is : %s The Corresponding 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 char
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;
break;
}
/* 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 folder/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/)
run command :
gcc -o enc encrypt.c -std=c99 -lcrypto -ldl
Note : You may faced compilation and Linker errors, if Open ssl is not install correctly
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...