Question

Your assignment: 1) Ask the user if we are encrypting or decrypting. 2) Ask the user...

Your assignment: 1) Ask the user if we are encrypting or decrypting. 2) Ask the user to enter a sentence to be transformed. 3) Ask the user to enter a sentence that will be used as the encryption or decryption key. 4) The sentences (array of characters) should end with a NULL terminator '\0'. 5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32, up to and including a ~ - Value 126. Therefore, the number of symbols is 95 (0 to 94), i.e. Mod 95. 6) If the key is shorter than the message, make sure the key repeats until it matches the length of the text to be transformed. 7) Print out the transformed (encrypted or decrypted) message. has to be written in c

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

// Function to encrypt the string
void encrypt(char *str, char *key, int size)
{
   int i;
   char ch;
   for(i=0; i<size; i++)
   {
       // XOR characters from str with the key
       ch = str[i] ^ key[i];
       str[i] = ch;
   }
   //Write a null byte at end of the string to make printing of strings proper
   str[size] = '\0';
}

// Function to decrypt the string
void decrypt(char *str, char *key, int size)
{
   int i;
   char ch;
   for(i=0; i<size; i++)
   {
       // XOR characters from str with the key
       ch = str[i] ^ key[i];
       str[i] = ch;
   }
   //Write a null byte at end of the string to make printing of strings proper
   str[size] = '\0';
}


int main()
{
   char str[100], key[100];
   int choice, size;

   //Ask user for input.
   printf("Enter a string: ");
   fgets(str, sizeof(str), stdin);
   printf("Enter key : ");
   fgets(key, sizeof(key), stdin);

   //fgets() reads newline from input, eliminate newline present at the end
   str[strlen(str)-1] = '\0';
   key[strlen(key)-1] = '\0';

   // If key is shorter than the string, keep appending the key to itself
   while(strlen(str) > strlen(key))
   {
       size = strlen(str) - strlen(key);
       if(size > strlen(key))
           size = strlen(key);
       strncat(key, key, size);
   }

   // Read input from user for encrypt/decrypt
   printf("1.Encrypt 2.Decrypt\nEnter your choice: ");
   scanf("%d", &choice);

   size = strlen(str);
   if(choice == 1)
   {
       //Calling encrypt function
       encrypt(str, key, size);
       printf("Encrypted String: %s\n", str);
// uncomment below 2 lines. in sample output, I have added the decrypt function in the encrypt function, because some ascii characters within range 32-126 are not displayed in linux terminal. So calling decrypt function immediately to check whether encryption is proper or not. The decrypt function takes the encrypted string and key as argument and returns the original string.
       decrypt(str, key, size);
       printf("Decrypted String: %s\n", str);
   }
   else if(choice == 2)
   {
       //Calling decrypt function
       decrypt(str, key, size);
       printf("Decrypted String: %s\n", str);
   }
   else
       printf("Error, Please input a valid chioce.\n");

}


oot@Ma1 ware : /mnt/d/Programs # .oot@Malware : /mnt/d/Programs # ./a.out Enter a string: the quick brown fox jumps cover ala

Add a comment
Know the answer?
Add Answer to:
Your assignment: 1) Ask the user if we are encrypting or decrypting. 2) Ask the user...
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
  • C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or...

    C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or decrypting. 2) Ask the user to enter a sentence to be transformed. 3) Ask the user to enter a sentence that will be used as the encryption or decryption key. 4) The sentences (array of characters) should end with a NULL terminator '\0'. 5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32,...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • 5. We must assume that keys are not secure forever, and will eventually be discovered; thus...

    5. We must assume that keys are not secure forever, and will eventually be discovered; thus keys should be changed periodically. Assume Alioe sets up a RSA cryptosystem and announces N = 3403, e = 11. (a) Encrypt m = 37 using Alice's system (b) At some point. Eve discovers Alice's decryption exponent is d = 1491. Verify this (by decrypting the encrypted value of rn = 37). (c) Alice changes her encryption key to e = 31, Encrypt rn...

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

  • Please answer this problem without using iostream and without strings or arrays. Just use stdio.h and...

    Please answer this problem without using iostream and without strings or arrays. Just use stdio.h and math.h. 57677 1 Caesar's Cipher (70 points) The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party with- out access to the cryptographic key. It is named after Julius Caesar, who allegedly used it to protect messages of military significance. The encryption and decryption operations are simple shifts of the alphabet letters in a cyclic fashion....

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA...

    Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA {    private BigInteger phi; private BigInteger e; private BigInteger d; private BigInteger num; public static void main(String[] args) {    Scanner keyboard = new Scanner(System.in); System.out.println("Enter the message you would like to encode, using any ASCII characters: "); String input = keyboard.nextLine(); int[] ASCIIvalues = new int[input.length()]; for (int i = 0; i < input.length(); i++) { ASCIIvalues[i] = input.charAt(i); } String ASCIInumbers...

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