Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int encrypt (int n);
- int decrypt (int n);
- void clearbuffer ();
- int main ()
- {
- int done, n1;
- done = 0;
- n1 = 0;
- char input;
- while (done != 1)
- {
- printf("Please Enter an 'e' to encrypt, 'd' to decrypt, or a 't' to terminate the program: ");
- scanf("%c", &input);
- clearbuffer();
- if (input == 't')
- {
- done++;
- }
- else if (input == 'e')
- {
- printf("Enter a four digit integer to be encrypted: \n");
- scanf("%d", &n1);
- clearbuffer();
- printf("Encrypted number is: %d \n", encrypt(n1));
- }
- else if (input == 'd')
- {
- printf("Enter a four digit encrypted number: \n");
- scanf("%d", &n1);
- clearbuffer();
- printf("Decrypted number is: %d \n", decrypt(n1));
- }
- else
- {
- printf("Wrong input value \n");
- }
- }
- printf("**Program Terminated**");
- return 0;
- }
- int encrypt (int n)
- {
- int result, d1,d2,d3,d4, x;
- result = 0;
- x = 0;
- d1 = (n/1000 + 7) % 10;
- d2 = ((n/100 %10) + 7) % 10;
- d3 = ((n/10 %10) + 7) % 10;
- d4 = ((n %10) + 7) % 10;
- x = d1;
- d1 = d3;
- d3 = x;
- x = d2;
- d2 = d4;
- d4 = x;
- result = d1*1000 + d2*100 + d3*10 + d4;
- return result;
- }
- int decrypt (int n)
- {
- int result, d1,d2,d3,d4, x;
- result = 0;
- x = 0;
- d1 = (n/1000 + 3) % 10;
- d2 = ((n/100 %10) + 3) % 10;
- d3 = ((n/10 %10) + 3) % 10;
- d4 = ((n %10) + 3) % 10;
- x = d1;
- d1 = d3;
- d3 = x;
- x = d2;
- d2 = d4;
- d4 = x;
- result = d1*1000 + d2*100 + d3*10 + d4;
- return result;
- }
- void clearbuffer ()
- {
- int ch;
- while ((ch = getchar ()) != EOF && ch!= '\n')
- ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement