Advertisement
kadoel

Vigenere

Nov 9th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <process.h>
  5.  
  6. void vigenereCipher(char *,char *);
  7. void encipher();
  8. void decipher();
  9.  
  10. void main()
  11. {
  12.      int choice;
  13.      //loop takes choice from user and calles appropriate function
  14.      while(1)
  15.      {
  16.           printf("\n1. Encrypt Text\n");
  17.           printf("2. Decrypt Text\n");
  18.           printf("3. Exit\n");
  19.           printf("Enter Your Choice : ");
  20.           scanf("%d",&choice);
  21.           fflush(stdin);
  22.           if(choice == 3)
  23.                exit(0);
  24.           else if(choice == 1)
  25.                encipher();
  26.           else if(choice == 2)
  27.                decipher();
  28.           else
  29.                printf("Please Enter Valid Option.");
  30.      }
  31. }
  32.  
  33. void encipher()
  34. {
  35.      unsigned int i,j;
  36.      char input[257],key[33];
  37.      printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:\n ");
  38.      gets(input);
  39.      printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: ");
  40.      gets(key);
  41.      for(i=0,j=0;i<strlen(input);i++,j++)
  42.      {
  43.           //repeat the key if you are at end of it.
  44.           if(j>=strlen(key))
  45.           {
  46.                j=0;
  47.           }
  48.           //actual logic -> character from input + character from key % 26 is encrypted charater
  49.           printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
  50.      }
  51.  
  52. }
  53.  
  54. void decipher()
  55. {
  56.      unsigned int i,j;
  57.      char input[257],key[33];
  58.      int value;
  59.      printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:\n ");
  60.      gets(input);
  61.      printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
  62.      gets(key);
  63.      for(i=0,j=0;i<strlen(input);i++,j++)
  64.      {
  65.           //repeat the key if you are at end of it.
  66.           if(j>=strlen(key))
  67.           {
  68.                j=0;
  69.           }
  70.           //similar to encipher only difference is you need to subtract
  71.           value = (toupper(input[i])-64)-(toupper(key[j])-64);
  72.           //if value is negative. We have to rotate it backwards (like backwards from z,y,x)
  73.           //so add it to 26 (it's a negative value to adding will actually cause subtraction) to get original character.
  74.           if( value < 0)
  75.           {
  76.                value = 26 + value;
  77.           }          
  78.           printf("%c",65 + (value % 26));
  79.      }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement