Advertisement
theguyoveryonder

Untitled

Nov 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. char menu(void);
  5. char action(char option);
  6. void encypt();
  7. void decrypt();
  8.  
  9.  
  10.  
  11. char menu(void){
  12.   char option;
  13.   printf("Pick whether you would like to enycrypt a text or decrypt a text.\n");
  14.   printf("1 - encrypt\n");
  15.   printf("2 - decrypt\n");
  16.   printf("x - end");
  17.   scanf(" %c", &option);
  18.   return option;
  19. }
  20. char action(char option){
  21. switch(option){
  22.   case '1':
  23.   encypt();
  24.   case '2':
  25.   decrypt();
  26. }
  27. }
  28.  
  29.  
  30. void encrypt(){
  31.   char message_1[100], ch,message_2[100];
  32.     int i, key;
  33.    
  34.     printf("Enter a message to encrypt: ");
  35.     fgets(message_1,100,stdin);
  36.     printf("Enter key: ");
  37.     scanf("%d", &key);
  38.    
  39.     for(i = 0; message_1[i] != '\0'; ++i){
  40.         ch = message_1[i];
  41.        
  42.         if(ch >= 'a' && ch <= 'z'){
  43.             ch = ch + key;
  44.            
  45.             if(ch > 'z'){
  46.                 ch = ch - 'z' + 'a' - 1;
  47.             }
  48.            
  49.             message_1[i] = ch;
  50.         }
  51.         else if(ch >= 'A' && ch <= 'Z'){
  52.             ch = ch + key;
  53.            
  54.             if(ch > 'Z'){
  55.                 ch = ch - 'Z' + 'A' - 1;
  56.             }
  57.            
  58.             message_1[i] = ch;
  59.         }
  60.     }
  61.    
  62.    printf("Encrypted message: %s", message_1);
  63. }
  64. void decrypt(){
  65.   char ch,message_2[100];
  66.     int i, key;
  67.   printf("Enter a message to decrypt: \n");
  68.     fgets(message_2,100,stdin);
  69.    
  70.    
  71.     for(i = 0; message_2[i] != '\0'; ++i){
  72.         ch = message_2[i];
  73.        
  74.         if(ch >= 'a' && ch <= 'z'){
  75.             ch = ch - key;
  76.            
  77.             if(ch < 'a'){
  78.                 ch = ch + 'z' - 'a' + 1;
  79.             }
  80.            
  81.             message_2[i] = ch;
  82.         }
  83.         else if(ch >= 'A' && ch <= 'Z'){
  84.             ch = ch - key;
  85.            
  86.             if(ch < 'A'){
  87.                 ch = ch + 'Z' - 'A' + 1;
  88.             }
  89.            
  90.             message_2[i] = ch;
  91.         }
  92.     }
  93.     printf("Enter key: ");
  94.     scanf("%d", &key);
  95.     printf("Decrypted message: %s", message_2);
  96.  
  97. }
  98. int main(){
  99.     char option;
  100.     printf("Encryption and decription program\n");
  101.     printf("-------------------------------------\n");
  102.    
  103.     do{
  104.       while(option != 'x'){      
  105.       if(option == '1'){
  106.         do(encypt());
  107.       }else{
  108.         do(decrypt());
  109.       }
  110.     }
  111.     }
  112.    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement