Background:
For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce.
There is an important catch, however: your program is going to be left intentionally vulnerable to a format string attack (also explained below). Though it will still “work” as intended if it is used appropriately with the expected input, it should be possible to exploit the program in order to produce something other than the desired output. In short, your program will be “hackable.”
Command-Line Input:
If you have not yet written a program that takes arguments at run-time via the command line, this will be worth exploring before starting the rest of the assignment. In short, it is possible to redefine main() in such a way that it can take input when the program is initially run. Your new definition of main should look something like this:
int main(int argc, char** argv) {
// your code below
}
Here, the integer “argc” represents the counter of arguments (counting ./a.out as the first argument), and the string array “argv” stores those arguments. For example, if the program was run using this command:
./a.out myArg1 myArg2 myArg3
Then argc would be equal to 4 (representing the four arguments, with ./a.out being the first and myArg3 being the last), argv[0] would store “./a.out”, argv[1] would store “myArg1”, argv[2] would store “myArg2”, and so on.
Your program should take one additional argument, representing the word to be encrypted. That means it should be run like this:
./a.out thisIsTheWordThatMyProgramWillEncrypt
Note here that argc == 2 and argv[1] == “thisIsTheWordThatMyProgramWillEncrypt” (of course, any string will work here as long as it does not contain spaces).
For additional help with taking command line arguments in C, see the following resource: https://www.geeksforgeeks.org/find-largest-among-three-different-positive-numbers-using-command-line-argument/ (Links to an external site.)
The Encryption Algorithm:
You will implement a simple XOR encryption against the word that is passed in as a command-line argument. If you are unfamiliar with XOR in general or in C, please see the following resource before continuing with this assignment:
https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/ (Links to an external site.)
Your encryption algorithm will work as follows:
Anything similar to the above can work, as long as it is a valid hexadecimal integer that can represent an English word or phrase. This will help the graders to easily identify your key when it appears later on.
int key = 0xBA55C1EF
for i from 0 to the length of the string:
string[i] = string[i] ^ key
Format String Attack:
A format string attack is a type of injection attack that takes advantage of undefined behavior in the C language specification for printf() (the same printf() you’ve been using all semester). For this program, you must write your final printf() statement in such a way that it is vulnerable to a format string attack. To get you started, see the following reference:
https://www.geeksforgeeks.org/format-string-vulnerability-and-prevention-with-example/ (Links to an external site.)
Output:
Your program will only produce a single line of output, which in most cases is the newly encrypted string. To receive full credit, however, you must print your string in such a way that it is vulnerable to the format string attack described above. To verify that your program is working, try to run it with a format string attack of your own: you will know that it is working when they secret key you declared inside your program is clearly visible in the console.
Deliverables:
Submit, as a zipped folder, both your C source code (the .c file), as well as two screenshots of your output. The first will represent your program running under normal conditions (i.e., it is given a normal word as an argument and simply prints the encrypted output). The second should demonstrate that your program is vulnerable to the format string attack, and should include your encryption key somewhere in the visible output.
Code in C
Read comment for a better explanation
#include<stdio.h>
#include<string.h>
//taking input from command line
int main(int argc, char *argv[])
{
//check if word is given as input or not by checking argc
if(argc<2)
{
printf("Enter word to be encrypted.\n");
return 0;
}
//Now argv[1] will contains word which need to be encrypted
//Define secret key in hexadecimal
int key = 0xBA55C1EF;
//To store encrypted text
char buffer[100];
//loop over argv[1] and encrypt every character using xor
operation i.e., char ^ key
int i,buff_len = 0; //length of buffer
for(i=0;i<strlen(argv[1]);i++)
{
buffer[buff_len++] = argv[1][i] ^ key; //perform xor
operation
}
//append null at last
buffer[buff_len] = '\0';
//For vulnerable to a format string attack we should write
printf() without format string (%s)
printf("Encrypted: ");
printf(buffer);
//For decryption NOT asked in question just for validation
char dec[100];
int dec_len = 0;
for(i=0;i<buff_len;i++)
dec[dec_len++] = buffer[i] ^ key;
printf("\nDecrypted: ");
printf(dec);
return 0;
}
Output

Background: For this assignment, you will write a small encryption utility that implements a simple encryption...
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...
Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...
In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A: Three two one And then it would display The smallest string was:...
C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...
Write a python program: Ask for the encryption key. The key must be a list of numbers between 0 and N-1, where N is the number of entries in the key's list; for example, 5,4,3,2,1,0 has six numbers, and each digit 0-5 appears in the list. Asks for a line of text to encrypt. Use the transposition key to transpose each chunk of N letters in input text. If the input text is not a multiple of the key length,...
Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as...
T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...
Hi, need this question ansered in c++, has multiple levels will
post again if you can complete every level so keep an eye out for
that.
here is a sketch of the program from the screenshot
int main (int argc, char** argv) { enum { total, unique } mode =
total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) {
switch(c) { case 't': mode = total; break; case 'u': mode = unique;
break; } } argc -=...