Question

In: Computer Science

SOLVE IN C: Playing with encryption: Write a program that will read a four-digit integer entered...

SOLVE IN C: Playing with encryption:

Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to obtain the original number. Print out both the encrypted and decrypted integer.

need help finishing code on encryption in C I was assigned to encrypt and then decrypt a number by reversing the process to get it's original number. Here are the original instructions from my teacher:

I was able to encrypt, but now I am struggling with how to decrypt it back to it's original number. Here is my code:

You'll see my comment where I say I can't figure it out from a certain point and where the code needs to be fixed in bold.

#include
int main(){

int digit; // stores initial 4 digit integer value.
int digit1; // used to retrieve first digit.
int digit2; // retrieve second digit.
int digit3; // retrieve third digit.
int digit4; // retrieve fourth digit.
int swappedDigit; // the new encrypted digit value.

int decry; // stores initial 4 digit integer value.
int decry1; // first digit retrieval.
int decry2; // second digit retrieval.
int decry3; // third digit retrieval.
int decry4; // fourth digit retrieval.
int decrypt; // final decrypted digit.


printf("Enter a 4 digit number:\n");
scanf("%d", &digit);

/* the following 4 lines isolate each digit, as well as adding 7 to them and getting the remainder.
*/
digit1 = digit /1000 % 10;
digit1 = (digit1 + 7) % 10;


digit2 = digit /100 % 10;
digit2 = (digit2 + 7) %10;


digit3 = digit /10 % 10;
digit3 = (digit3 + 7) %10;


digit4 = digit % 10;
digit4 = (digit4 + 7) %10;

/* this formula below swaps the second and fourth numbers of the new decimal number from the formula above.
This gives us the new encrypted number. */

swappedDigit = (digit1 * 1000) + (digit2) + (digit3 * 10) + (digit4 * 100);

// printing the original number and the new encrypted number stored as "swappedDigit".
printf("The original digit is %d\n", digit);
printf("The encrypted digit is %d\n", swappedDigit);

// alerting the user to now input the same number to decrypt it by reversing the process.

printf("Now time to reverse the process.\n");
printf("Enter a 4 digit encrypted number:\n");
scanf("%d", &decry);


// this bottom part is all wrong. Not sure how to reverse it from here. Adding 3 only works for some numbers.

decry1 = decry /1000 % 10 + 3;
decry2 = decry /100 % 10 + 3;
decry3 = decry /10 % 10 + 3;
decry4 = decry % 10 + 3;

// swapping the second and fourth digits back to where they were originally.

decrypt = (decry1 * 1000) + (decry2) + (decry3 * 10) + (decry4 * 100);

// finally, printing the original decrypted number.

printf("The decrypted number is %d", decrypt);


   return 0;
}

Solutions

Expert Solution

#include <stdio.h>

int main()

{

    int digit;        // stores initial 4 digit integer value.

    int digit1;       // used to retrieve first digit.

    int digit2;       // retrieve second digit.

    int digit3;       // retrieve third digit.

    int digit4;       // retrieve fourth digit.

    int swappedDigit; // the new encrypted digit value.

    int decry;        // stores initial 4 digit integer value.

    int decry1;       // first digit retrieval.

    int decry2;       // second digit retrieval.

    int decry3;       // third digit retrieval.

    int decry4;       // fourth digit retrieval.

    int decrypt;      // final decrypted digit.

    printf("Enter a 4 digit number:\n");

    scanf("%d", &digit);

    /* the following 4 lines isolate each digit, as well as adding 7 to them and getting the remainder.

*/

    digit1 = digit / 1000 % 10;

    digit1 = (digit1 + 7) % 10;

    digit2 = digit / 100 % 10;

    digit2 = (digit2 + 7) % 10;

    digit3 = digit / 10 % 10;

    digit3 = (digit3 + 7) % 10;

    digit4 = digit % 10;

    digit4 = (digit4 + 7) % 10;

    /* this formula below swaps the second and fourth numbers of the new decimal number from the formula above.

This gives us the new encrypted number. */

    swappedDigit = (digit1 * 1000) + (digit2) + (digit3 * 10) + (digit4 * 100);

    // printing the original number and the new encrypted number stored as "swappedDigit".

    printf("The original digit is %d\n", digit);

    printf("The encrypted digit is %d\n", swappedDigit);

    // alerting the user to now input the same number to decrypt it by reversing the process.

    printf("Now time to reverse the process.\n");

    printf("Enter a 4 digit encrypted number:\n");

    scanf("%d", &decry);

    // this bottom part is all wrong. Not sure how to reverse it from here. Adding 3 only works for some numbers.

    digit1 = decry /1000 % 10;

    digit1 = (digit1 + 3) % 10;

    digit2 = decry /100 % 10;

    digit2 = (digit2 + 3) %10;

    digit3 = decry /10 % 10;

    digit3 = (digit3 + 3) %10;

    digit4 = decry % 10;

    digit4 = (digit4 + 3) %10;

    

    // swapping the second and fourth digits back to where they were originally.

    decrypt = (digit1 * 1000) + (digit2) + (digit3 * 10) + (digit4 * 100);

    // finally, printing the original decrypted number.

    printf("The decrypted number is %d\n", decrypt);

    return 0;

}

Let me know if you have any clarifications. Thank you...


Related Solutions

Playing with encryption: Write a program that will read a four-digit integer entered by the user...
Playing with encryption: Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to obtain the original...
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data,...
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data, determines whether all the digits of the number are even. For example, if the number were 5688, it would not meet the condition since the most significant digit (5) is odd; if on the contrary, the number were 6244, it would be true, since all the digits are even. 5. Make the flowchart and the C ++ program that, when receiving N integers as...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT