My C program is supposed to make a random key the same length of clear_text string and then use the random key to encrypt clear_text, but I can’t get it to work.



The reason it failed is
- clear_text, key, encrypted are all declared as pointers. You have
not assigned memory to it. You should either declare them as
clear_text[20] etc or char *clear_text = malloc(sizeof(char)*20); I
have given them a size while declaring.
- The encrypted string did not end with \0 in your code. Fixed that.
The fixed code is given below
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char* make_rand_key(int length, char* key);
void encrypt();
int main(){
encrypt();
}
char* make_rand_key(int length, char* key){
int range = 78;
int max = 48;
char c = 'a';
int i;
srand(time(NULL));
for(i = 0; i < length; i++){
c = rand() % range + max;
key[i] = c;
}
key[i] = '\0';
return key;
}
void encrypt(){
char clear_text[30] = "Hello everybody";
int length = 0;
while(clear_text[length] != '\0')
length++;
char key[30];
char encrypted[30];
int x;
for(x = 0; x < length; x++){
encrypted[x] = clear_text[x] ^
key[x];
}
encrypted[length] = '\0';
printf("%s\n", encrypted);
}
My C program is supposed to make a random key the same length of clear_text string...
I have a problem with my C code. Its suppose to print a random
string of length 10 and sometimes it does, but sometimes it prints
a shorter string.
int main() { int len = 10; char key[len+1]; srand(time(0)); int i; for (i=0; i<len; i++) { key[i] = rand()%256; if (key[i] == EOF || key[i] == '\0'). key[i] = 1; key[i] = '\0'; printf("The generated key is %s\n", key);
The following code is a C Program that is written for encrypting
and decrypting a string. provide a full explanation of the working
flow of the program.
#include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...
OPERATING SYSTWM
Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...
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...
9. The purpose of the following program is to generate 10 random integers, store them in an array, and then store in the freq frequency array, the number of occurrences of each number from 0 to 9. Can you find any errors in the program? If yes, correct them. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int main(void) int i, num, arr[SIZE], freq[SIZE]; srand (time (NULL)); arr[1] rand(); SIZE; for(i i 〈 i++) num arr[i]; freq[num]++; printf("InNumber occurrences...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
Question: For the picture writing question, if the question says that the picture length (height) and width are multiples of 5, then be prepared (for example) to handle a situation where you are being asked to blacken the fourth (vertical) strip from the left. Code: #include #include #include #include #define BUFFER_SIZE 70 #define TRUE 1 #define FALSE 0 int** img; int numRows; int numCols; int maxVal; FILE* fo1; void addtopixels(int** imgtemp, int value); void writeoutpic(char* fileName, int** imgtemp); int** readpic(char*...
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();...
c++
language
Step 1. Try to execute the following program (CP11E02) and record the results. #include <iostream.h> #include <string.h> class tv show private: char title[30]; int length; // 30 or 60 minutes char kind; // Ccomedy, d-drama, i - infomercial public: tv_show(char til),int I, char k) // constructor (strcpy(title, s); length = 1 kind=k; void main(void) tv show one("One Day to Live", 60,'d'); tv show two("Enemies".30,'c'), Step 2. Modify the statement tv_show one("One Day to Live",60,'d'); to read tv_show one;...