Advertisement
Guest User

For educational purposes only

a guest
Nov 1st, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include <windows.h>
  8.  
  9. const char upper_alph[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  10. const char lower_alph[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  11.  
  12. void wait()
  13. {
  14.     for (int i= 0; i<=100; i++)
  15.     {
  16.         printf("\n\n\n\n\n\n\n\n\n");
  17.         printf("\t ============================================================ \n");
  18.         printf("\t ||  ____________________________________________________  || \n");
  19.         printf("\t || |                                                    | || \n");
  20.         printf("\t                     Please Wait    %d %                      \n", i);
  21.         printf("\t || |____________________________________________________| || \n");
  22.         printf("\t ||                                                        || \n");
  23.         printf("\t ============================================================ \n");
  24.         system ("cls");
  25.     }Sleep(200);
  26. }
  27.  
  28. void thank();
  29. {
  30.     int i,j,k,m;
  31.     char kata[10][10]={{'T','H','A','N','K'},{'Y','O','U'},{'A','N','D'}};
  32.     printf("\t\t\t");
  33.     for(i=0; kata[i][0]!='\0';i++)
  34.     {}
  35.     for(j=0;j<=i;j++)
  36.     {
  37.         for(k=0;kata[j][k]!='\0';k++)
  38.         {}
  39.         for(m=0;m<=k-1;m++)
  40.         {
  41.             printf("%c",kata[j][m]);Sleep(100);
  42.         }
  43.         printf(" ");Sleep(100);
  44.     }
  45. }
  46. void emoticon()
  47. {
  48.     int i,j,k,m;
  49.     char kata[10][10]={{'H','O','P','E','F','U','L','L','Y'},{'U','S','E','F','U','L'},{'^','_','^'}};
  50.     for(i=0; kata[i][0]!='\0';i++)
  51.     {}
  52.     for(j=0;j<=i;j++)
  53.     {
  54.         for(k=0;kata[j][k]!='\0';k++)
  55.         {}
  56.         for(m=0;m<=k-1;m++)
  57.         {
  58.             printf("%c",kata[j][m]);Sleep(100);
  59.         }
  60.         printf(" ");Sleep(100);
  61.     }
  62.     printf("\n");
  63. }
  64.  
  65. char num(char c)
  66. {
  67.     if(isupper(c))
  68.     {
  69.         for(int i = 0; i < 26; i++)
  70.             if(upper_alph[i] == c)
  71.                 return i;
  72.     }
  73.     else
  74.     {
  75.         for(int i = 0; i < 26; i++)
  76.             if(lower_alph[i] == c)
  77.                 return i;
  78.     }
  79.     return 0;
  80. }
  81.  
  82. void encrypt(char *message, int shift)
  83. {
  84.     int i = 0;
  85.     while(message[i] != NULL)
  86.     {
  87.         if(isalpha(message[i]))
  88.         {
  89.             if(isupper(message[i]))
  90.             {
  91.                 printf("%c", upper_alph[(num(message[i])+shift)%26]);
  92.             }
  93.             else
  94.             {
  95.                 printf("%c", lower_alph[(num(message[i])+shift)%26]);
  96.             }
  97.         }
  98.         else
  99.         {
  100.             printf("%c", message[i]);
  101.         }
  102.             i++;
  103.     }
  104. }
  105. void decrypt(char *message, int shift)
  106. {
  107.     int i = 0;
  108.     int iserror;
  109.     int error;
  110.     while(message[i] != NULL)
  111.     {
  112.         if(isalpha(message[i]))
  113.         {
  114.             if(isupper(message[i]))
  115.             {
  116.                 iserror=(num(message[i])-shift)%26+1;
  117.                 //printf("%i",iserror);
  118.                 if(iserror>0)
  119.                     printf("%c", upper_alph[(num(message[i])-shift)%26]);
  120.                 else
  121.                 {
  122.                     error=((num(message[i])-shift)%26)+26;
  123.                     //printf("%i",error);
  124.                     printf("%c", upper_alph[error]);
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 iserror=(num(message[i])-shift)%26+1;
  130.                 //printf("%i",iserror);
  131.                 if(iserror>0)
  132.                     printf("%c", lower_alph[(num(message[i])-shift)%26]);
  133.                 else
  134.                 {
  135.                     error=((num(message[i])-shift)%26)+26;
  136.                     //printf("%i",error);
  137.                     printf("%c", lower_alph[error]);
  138.                 }
  139.             }
  140.         }
  141.         else
  142.         {
  143.             printf("%c", message[i]);
  144.         }
  145.             i++;
  146.     }
  147. }
  148.  
  149. #define OK       0
  150. #define NO_INPUT 1
  151. #define TOO_LONG 2
  152.  
  153. static int getLine (char *prmpt, char *buff, size_t sz)
  154. {
  155.     int ch, extra;
  156.     // Get line with buffer overrun protection.
  157.     if (prmpt != NULL) {
  158.         printf ("%s", prmpt);
  159.         fflush (stdout);
  160.     }
  161.     if (fgets (buff, sz, stdin) == NULL)
  162.         return NO_INPUT;
  163.  
  164.     // If it was too long, there'll be no newline. In that case, we flush
  165.     // to end of line so that excess doesn't affect the next call.
  166.     if (buff[strlen(buff)-1] != '\n') {
  167.         extra = 0;
  168.         while (((ch = getchar()) != '\n') && (ch != EOF))
  169.             extra = 1;
  170.         return (extra == 1) ? TOO_LONG : OK;
  171.     }
  172.  
  173.     // Otherwise remove newline and give string back to caller.
  174.     buff[strlen(buff)-1] = '\0';
  175.     return OK;
  176. }
  177.  
  178. int main()
  179. {
  180.     system("color 02");
  181.     printf("\n\n #####\n");                                                                            
  182.     printf("#     # #####  #   # #####  #####  ####   ####  #####    ##   #####  #    #\n");
  183.     printf("#       #    #  # #  #    #   #   #    # #    # #    #  #  #  #    # #    #\n");  
  184.     printf("#       #    #   #   #    #   #   #    # #      #    # #    # #    # ######\n");  
  185.     printf("#       #####    #   #####    #   #    # #  ### #####  ###### #####  #    #\n");  
  186.     printf("#     # #   #    #   #        #   #    # #    # #   #  #    # #      #    #\n");  
  187.     printf(" #####  #    #   #   #        #    ####   ####  #    # #    # #      #    #\n");
  188.     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
  189.     printf("+                   Program Created by Vaisakh and Abdul                  +\n");
  190.     printf("+                                                                         +\n");
  191.     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
  192.     printf("+One of the many encryption programs that use Caesar Chiper Tecnique for  +\n");
  193.     printf("+encryption and decryption. This program using C++ language.              +\n");
  194.     printf("+                                                                         +\n");
  195.     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n\n");
  196.     printf("\n\n\n\n\nPress Any Key to Continue");
  197.     getche();
  198.     wait();
  199. main:
  200.     system("cls");
  201.     int rc, shift,i,j, again;
  202.     int opt;
  203.     char mes[1024],a;
  204.     int sh = 0;
  205.     printf("+==============================================================================+");
  206.     printf("|                       CRYPTOGRAPH [Caesar Chiper | C++]                      |");
  207.     printf("+------------------------------------------------------------------------------+");
  208.     printf("|                     Done by Vaisah K Nair and Abdul Razzak V                 |");                                        
  209.     printf("|                                                                              |");
  210.     printf("+==============================================================================+\n\n");
  211.     rc = getLine ("Enter message to be encrypted or decrypted: ", mes, sizeof(mes));
  212.     if (rc == NO_INPUT)
  213.     {
  214.         // Extra NL since my system doesn't output that on EOF.
  215.         printf ("\nNo input\n");
  216.         return 1;
  217.     }
  218.     if (rc == TOO_LONG) {
  219.         printf ("Input too long [%s]\n", mes);
  220.         return 1;
  221.     }
  222.     j=strlen(mes);
  223. back_1:
  224.     printf("\n\nPlease, Select One Option\n");
  225.     printf("1. Encryption\n");
  226.     printf("2. Decryption\n");
  227.     printf("Your Option : ");
  228.     scanf("%i",&opt);
  229.     printf("\n\nHow Much Shift (example : 1) : ");
  230.     scanf("%i",&shift);
  231.     switch(opt){
  232.     case 1 :
  233. encryp:
  234.         system("cls");
  235.         printf("+==============================================================================+");
  236.         printf("|                CRYPTOGRAPH [Caesar Chiper | C++] | ENCRYPTION                |");
  237.         printf("+------------------------------------------------------------------------------+");
  238.         printf("|             Done by Vaisah K Nair and Abdul Razzak V                         |");
  239.         printf("+==============================================================================+\n\n");
  240.         printf("RESULT : \n\n");
  241.         encrypt(mes, shift);
  242.         printf("\n\n\nYOUR INPUT :\n\n");
  243.         for(i=0;i<j;i++)
  244.         {
  245.             printf("%c", mes[i]);
  246.         }
  247.         break;
  248.     case 2:
  249. decryp:
  250.         system("cls");
  251.         printf("+==============================================================================+");
  252.         printf("|                CRYPTOGRAPH [Caesar Chiper | C++] | DECRYPTION                |");
  253.         printf("+------------------------------------------------------------------------------+");
  254.         printf("|                     Done by Vaisah K Nair and Abdul Razzak V                    |");
  255.         printf("+==============================================================================+\n\n");
  256.         printf("RESULT : \n\n");
  257.         decrypt(mes, shift);
  258.         printf("\n\n\nYOUR INPUT :\n\n");
  259.         for(i=0;i<j;i++)
  260.         {
  261.             printf("%c", mes[i]);
  262.         }
  263.         break;
  264.     default :
  265.         printf("Your Selection is Wrong.\n Please, Select Again\n");
  266.         getche();
  267.         goto back_1;
  268.         break;
  269.     }
  270.     printf("\n\n+==============================================================================+");
  271.     printf("\nPlease, Select One Option\n");
  272.     printf("1. Main Menu\n");
  273.     printf("2. ");
  274.     if(opt==1)
  275.     {
  276.         printf("Decryption (");
  277.         opt=2;
  278.     }
  279.     else
  280.     {
  281.         printf("Encryption (");
  282.         opt=1;
  283.     }
  284.     if(j>10)
  285.     {
  286.         for(i=0;i<=10;i++)
  287.         {
  288.             printf("%c", mes[i]);
  289.         }
  290.         printf("...");
  291.     }
  292.     else
  293.     {
  294.         for(i=0;i<=j;i++)
  295.         {
  296.             printf("%c", mes[i]);
  297.         }
  298.     }
  299.     printf(")\n3. Exit\n");
  300.     printf("Your Opt : ");
  301.     scanf("%i",&again);
  302.     scanf("%c",&a);
  303.     if(again==1)
  304.     {
  305.         goto main;
  306.     }
  307.     else if(again==2)
  308.     {
  309.         if(opt==1)
  310.             goto encryp;
  311.         else
  312.             goto decryp;
  313.     }
  314.     else
  315.     {
  316.         thank();
  317.         emoticon();
  318.     }
  319.     fflush(stdin);
  320.     getchar();
  321.     return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement