Question

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.

A wΝΗ 1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 char* make_rand_key(int length, char* key); 5 void encry10 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<lenSegmentation fault ... Program finished with exit code 139 Press ENTER to exit console.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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);
}

Add a comment
Know the answer?
Add Answer to:
My C program is supposed to make a random key the same length of clear_text string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a problem with my C code. Its suppose to print a random string of...

    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....

    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) {...

  • 9. The purpose of the following program is to generate 10 random integers, store them in...

    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...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    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...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    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...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    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...

    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 Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    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();...

  • Question: For the picture writing question, if the question says that the picture length (height) and...

    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*...

  • Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a...

    Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a hashmap as the underlying data structure. Inspiration is ok, however the implementation must be yours. The hashmap must be able to grow to fit new elements, but it does not need to reduce its size. -- Linked lists? Positional arrays? All are fine. - Keys and values are strings (char *) - Operations are: get, put Provided files: - Implement the contract set in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT