I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work.
In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me know if you need more information).
An example of running the program on a linux machine.
The program should:
Accept as input from “stdin” a file of data. Please do not open the file, just read from the standard input. The first 16 bytes are as shown below in the example, four characters for row zero, four more for row one, etc. After this initial string there is a newline character, and then the data to encrypt. You can read these (and everything else) with “getchar”, and print everything with just “putchar”.
We need to start doing error checking. If there’s fewer than 16 bytes on the first line, or greater than 16 bytes on the first line, print an appropriate error message and abandon ship. If things look OK, keep going to ...
Output the original 16 bytes as well as the newline character.
For each byte of the rest of the data, encrypt the byte and print it, then after each byte rotate the table 90 degrees left (counterclockwise).
Repeat this byte by byte until you encounter the end of the file.
You must use the following functions for this program:
main – in charge of running the show.
rotate – it takes one parameter, the 4x4 table, and rotates it 90 degrees left. It is a “void” function.
encrypt – it takes two parameters, the character to encrypt and the table. It returns a “char” as the result.
Make sure the program compiles with the following:
gcc -Wall -pedantic -std=c1x -o tablecrypt tablecrypt.c
Encrypting one byte can be done with something like this.
Suppose that you need to flip two bits, say bit 7 and bit 6. Obviously if both are zero there's nothing you have to do, and if both are 1, same story. Only for 01 or 10 do you need to invert them, and this can be done with XOR. So for bits 7 and 6, and variable X, something like this ought to be fine:
if ( ( X & 0xc0 == 0x80 ) || ( X & 0xc0 == 0x40 ) )
X ^= 0xc0;
So if you had
SNSS NSNS SNNS SNNS (SNSSNSNSSNNSSNNS)
01 01 00 10 (01010010)... it would become
10 01 00 10 (10010010).
You can count the number of S's in every 4, and if there are an odd number of S's, then swap. If it is even like SNNS then it won't be changed.
HERE IS THE CODE I HAVE THAT I DONT KNOW HOW TO FIX OR WHAT IS WRONG.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//function to convert char into its binary
void getBinaryFromChar(char c, char binary[])
{
for (int i = 7; i >= 0; --i)
{
binary[7 - i] = ((c & (1 << i)) ? '1' : '0');
}
}
//function to convert binary into its character
char getCharFromBinary(char binary[])
{
int n = 0;
for(int i = 0; i < 8; i++)
{
if(binary[i] == '1')
{
n += pow(2, 7 - i);
}
}
n %= 128;
char c = (char)n;
return c;
}
//function to encrypt the char
char encrypt(char binary[], char table[4][4])
{
int count = 0;
// loop for the encryption
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(table[i][j] == 'S')
{
count++;
}
}
//if count is odd then swap
if(count % 2)
{
char ch = binary[2 * i];
binary[2 * i] = binary[2 * i + 1];
binary[2 * i + 1] = ch;
}
count = 0;
}
//return the encrypted character
return getCharFromBinary(binary);
}
//function to rotate the table 90 degree left
void rotateTable(char table[4][4])
{
int N = 4;
for (int x = 0; x < N / 2; x++)
{
for (int y = x; y < N - x - 1; y++)
{
int temp = table[x][y];
table[x][y] = table[y][N - 1 - x];
table[y][N - 1 - x] = table[N - 1 - x][N - 1 - y];
table[N - 1 - x][N - 1 - y] = table[N - 1 - y][x];
table[N - 1 - y][x] = temp;
}
}
}
//main function to implement the encryption of data
int main()
{
int count = 0;
char ch, string[16];
//loop to store the first 16 characters
while((ch = getchar()) != '\n')
{
string[count] = ch;
count++;
putchar(ch);
if(count == 16)
{
break;
}
}
//condition to check that char before newline are exactly 16 or not
if(count == 16 && getchar() == '\n')
{
putchar('\n');
}
else
{
printf("Data is corrupted can't encrypt the data\n");
exit(0);
}
//create table for the first 16 characters
char table[4][4];
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
table[i][j] = string[i * 4 + j];
}
}
//to store the binary value of the characters
char binary[8],encryptedch;
//loop to read each byte or character one by one until the EOF
while((ch = getchar()) != EOF)
{
//function to get the binary value of ch into binary string
getBinaryFromChar(ch, binary);
//encrypt function returns the encrypted value of ch character
encryptedch = encrypt(binary, table);
//print the encrypted charater
printf("%c", encryptedch);
//rotate the table to 90 degree left each time
rotateTable(table);
}
//all the data is encrypted now
}
The program appears to encrypt correctly. But not decrypt. You should be able to pipe in the output of the initial encryption as input and see the original text. Any help is very much appreciated please I am stuck on this.
I guess you have to declare the arrays you are using as global, else how are you updating the values. This may be the only problem with your code. So I will request you to declare the arrays globally,then modify the code according to it. Rest everything looks fine.
I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...
12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...
Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...
I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...
Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...
I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...
C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...
I need help implementing the following code without using filehandling and only getchar(); this is the prompt "The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no...
Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) { int ch, i=0; while((ch =getchar()) != '\n') if(1<n) str[i++]==ch; str[i] != '\0'; return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...