Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int encrypt(int num);
  4. int decrypt(int encryptedNum);
  5.  
  6. int encryptMenu(void);
  7. int decryptMenu(void);
  8.  
  9. int main(void)
  10. {
  11.     char choice = '0';
  12. START:
  13.     printf("%s\n\n%s\n%s\n\n%s", "Do you want to encrypt or decrypt a code?",
  14.            "[E\\e] - To Encrypt",
  15.            "[D\\d] - To Decrypt",
  16.            "Your answer: ");
  17.     scanf("%c", &choice);
  18.     switch (choice)
  19.     {
  20.     case 'e':
  21.     case 'E':
  22.         encryptMenu(); // prints encryption menu
  23.         break;
  24.     case 'd':
  25.     case 'D':
  26.         decryptMenu(); // prints decryption menu
  27.         break;
  28.     default:
  29.         puts("Type in either 'e' or 'd'.");
  30.         goto START;
  31.         break;
  32.     }
  33. }
  34.  
  35. int encrypt(int num)
  36. {
  37.     int d1, d2, d3, d4; // 4 digits of the number to be separated
  38.     int result;         // encrypted integer to be returned
  39.     // Separate digits
  40.     d4 = num % 10;
  41.     num /= 10;
  42.     d3 = num % 10;
  43.     num /= 10;
  44.     d2 = num % 10;
  45.     num /= 10;
  46.     d1 = num;
  47.     // Add 7 to each
  48.     d1 += 7;
  49.     d2 += 7;
  50.     d3 += 7;
  51.     d4 += 7;
  52.     // Get the remainder when divided by 10
  53.     d1 %= 10;
  54.     d2 %= 10;
  55.     d3 %= 10;
  56.     d4 %= 10;
  57.     // Swap digit 1 with 3, and 2 with 4
  58.     result = d3 * 1000 + d4 * 100 + d1 * 10 + d2;
  59.     return result;
  60. }
  61. int decrypt(int encryptedNum)
  62. {
  63.     int d1, d2, d3, d4; // 4 digits of the number to be separated
  64.     int result;         // encrypted integer to be returned
  65.     // Separate digits
  66.     d4 = encryptedNum % 10;
  67.     encryptedNum /= 10;
  68.     d3 = encryptedNum % 10;
  69.     encryptedNum /= 10;
  70.     d2 = encryptedNum % 10;
  71.     encryptedNum /= 10;
  72.     d1 = encryptedNum;
  73.     // Add 10, if the number was previously divided
  74.     if (d1 < 8 && d1 > 0)
  75.         d1 += 10;
  76.     if (d2 < 8 && d2 > 0)
  77.         d2 += 10;
  78.     if (d3 < 8 && d3 > 0)
  79.         d3 += 10;
  80.     if (d4 < 8 && d4 > 0)
  81.         d4 += 10;
  82.     // Substract 7 from each
  83.     d1 -= 7;
  84.     d2 -= 7;
  85.     d3 -= 7;
  86.     d4 -= 7;
  87.     // Swap digit 1 back with 3, and 2 with 4
  88.     result = d3 * 1000 + d4 * 100 + d1 * 10 + d2;
  89.     return result;
  90. }
  91.  
  92. int encryptMenu(void)
  93. {
  94.     int input;
  95.     printf("\n%s", "Enter a 4-digit integer: ");
  96.     scanf("%d", &input);
  97.     if (input < 1000 || input > 9999) // make sure input is a 4-digit integer
  98.     {
  99.         puts("4-digit integer is required!");
  100.         encryptMenu(); // go back and ask for an input again
  101.     }
  102.     printf("\nYour encrypted integer is: %d\n\n", encrypt(input));
  103.     main();
  104. }
  105. int decryptMenu(void)
  106. {
  107.     int input;
  108.     printf("\n%s", "Enter an encrypted 4-digit integer: ");
  109.     scanf("%d", &input);
  110.     if (input < 1000 || input > 9999) // make sure input is a 4-digit integer
  111.     {
  112.         puts("4-digit integer is required!");
  113.         decryptMenu(); // go back and ask for an input again
  114.     }
  115.     printf("\nYour encrypted integer is: %d\n\n", decrypt(input));
  116.     main();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement