Question

I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key

My Current Code is:

void SubDecrypt(char *message, char *encryptKey) {
int iteration;
int iteration_Num_Two = 0;
int letter;
printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key
scanf("%s", encryptKey);                                                                   //Input the Encryption key
for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end
{
letter = message[iteration];                                                       //Assign the first character of message to letter
if (letter >= 'A' && letter <= 'Z')                                           //Check the letter is between 'A' to 'Z' i.e/ upper case character or not
{
letter = letter - 32;                                               //Move the letter to 32 characters back
}
if (letter >= 65 && letter <= 90) {                                   //After subtraction if the letter lies in the range 65 to 90(inclusive)
  
for (iteration_Num_Two = 0; iteration_Num_Two < 27; iteration_Num_Two++) //loop will continue from 0 to 26
{
if (message[iteration] == encryptKey[iteration_Num_Two])       //Check if the message[iteration] and encryptKey[iteration_Num_Two] will equal then terminate the loop
{
break;
}
}
message[iteration] = 'A' + iteration_Num_Two;                           //Set the message[iteration] to the value of iteration_Num_Two+'A'(uppercase) i.e/ 65+iteration_Num_Two
  
}
printf("%s\n", message);                                                           //Display the message

}
;
printf("CipherText message: %s\n", message);                                                   //Display the cipher text
printf("%s\n", encryptKey);                                                                   //Display the encryptKey
}

The Output is:

Please Enter the The Text You Wish to Encrypt/Decrypt: SCGGW Select option to be completed 1. Encrypt the entered mesaage wit

PLEASE BE ADVISED IVE ONLY PROVIDED MY CODE FOR THE FUNCTION NOT MY MAIN USER CODE. COULD YOU PLEASE SHOW ME WHERE IVE GONE WRONG

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

You have made two or three mistakes , all these mistakes have been corrected in the below code

#include<stdio.h>

void SubDecrypt(char *message, char *encryptKey) {
int iteration;
int iteration_Num_Two = 0;
int letter;
printf("Enter Encryption Key: \n"); //Display the message to enter encryption key
scanf("%s", encryptKey); //Input the Encryption key
for (iteration = 0; message[iteration] != '\0'; iteration++) //loop will continue till message reaches to end
{
letter = message[iteration]; //Assign the first character of message to letter
if (letter >= 'a' && letter <= 'z') //Check the letter is between 'a' to 'z' i.e/ lower case character or not
{
letter = letter - 32; //Move the letter to 32 characters back for converting into upper case letter
}
if (letter >= 65 && letter <= 90) { //After subtraction if the letter lies in the range 65 to 90(inclusive)
  
for (iteration_Num_Two = 0; iteration_Num_Two < 27; iteration_Num_Two++) //loop will continue from 0 to 26
{
if (letter == encryptKey[iteration_Num_Two]) //Check if the letter and encryptKey[iteration_Num_Two] will equal then terminate the loop
{
break;
}
}
if(message[iteration]>='a' && message[iteration]<='z') //WHEN our original message is small so convert to equivalent small letter
message[iteration]='a'+iteration_Num_Two;
else
message[iteration] = 'A' + iteration_Num_Two; //Set the message[iteration] to the value of iteration_Num_Two+'A'(uppercase) i.e/ 65+iteration_Num_Two
  
}
printf("%s\n", message); //Display the message

}
;
printf("CipherText message: %s\n", message); //Display the cipher text
printf("%s\n", encryptKey); //Display the encryptKey
}

int main()
{
   char message[]="SCGGW";
   char encryptKey[100];
   SubDecrypt(message,encryptKey);
}

you can compare with your code to find your mistakes , also the key is in upper case that is why i've added the code where you need to first convert lower case to upper case ( you were doing for uppper case) .

Also in your output you have entered a wrong key which contain two consecutive W so remove that one W .

the correct output is shown below

PS D: \mingw-w64 1686-8.1.0-posix-dwarf-rt_v6-revo\mingw32\bin> ./a.exe Enter Encryption Key: KIMQCDHSOFAGUJWYERPLTZBXNV HCGG

I hope this will help you so please give positive ratings :))

Add a comment
Know the answer?
Add Answer to:
I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...
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
  • Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(cha...

    Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey);    for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') {    letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...

  • Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message...

    Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message The code: #include<stdio.h> #include <stdlib.h> char arr[26][26]; char message[22], key[22], emessage[22], retMessage[22]; int findRow(char); int findColumn(char); int findDecRow(char, int); int main() { int i = 0, j, k, r, c; k = 96; for (i = 0; i<26; i++) { k++; for (j = 0; j<26; j++) { arr[i][j] = k++; if (k == 123) k = 97; } } printf("\nEnter message\n"); fgets(message, 22,...

  • C PROGRAMMING Implement a function (using only #include library) that can encrypt/decrypt with a substitution cipher....

    C PROGRAMMING Implement a function (using only #include library) that can encrypt/decrypt with a substitution cipher. cipher_sub (a, b, c, d) a is the string that has the data that will be encrypted or decrypted b is the string that has the result of the encrypt/decrypt c is the code string used for our substitution cipher (27 entries plus '\0' character) d is an integer that will pass two constants defined as ENC (encrypt) or DEC (decrypt) --> The function...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • in c++ The science of writing secret codes is called cryptography. For thousands of years cryptography...

    in c++ The science of writing secret codes is called cryptography. For thousands of years cryptography has made secret messages that only the sender and recipient could read, even if someone captured the messenger and read the coded message. A secret code system is called a cipher. In cryptography, we call the message that we want to be secret the plaintext. The plaintext could look like this:  Hello there! The keys to the house are hidden under the flower pot. Converting...

  • . Please write a function that will do the following Decryption of a message encrypted with a substitution cipher given cipher text only without any key Please provide comments on each line of code ou...

    . Please write a function that will do the following Decryption of a message encrypted with a substitution cipher given cipher text only without any key Please provide comments on each line of code outlining what that line is doing. Note: Only the Function is to be written, all user inputs i.e the text to be decrypted will be entered earlier in the program. Code must be written in C and be able to be compiled using GCC If any...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

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

  • 8.16 Ch 8, Part 1: XOR Cipher Write this program using Eclipse. Comment and style the...

    8.16 Ch 8, Part 1: XOR Cipher Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. This program will implement a simple XOR cipher on an integer array, where the data to be encoded is XOR'd with a key. This idea is often used...

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

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