In: Computer Science
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;
}
#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...