Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <process.h>
- void vigenereCipher(char *,char *);
- void encipher();
- void decipher();
- void main()
- {
- int choice;
- //loop takes choice from user and calles appropriate function
- while(1)
- {
- printf("\n1. Encrypt Text\n");
- printf("2. Decrypt Text\n");
- printf("3. Exit\n");
- printf("Enter Your Choice : ");
- scanf("%d",&choice);
- fflush(stdin);
- if(choice == 3)
- exit(0);
- else if(choice == 1)
- encipher();
- else if(choice == 2)
- decipher();
- else
- printf("Please Enter Valid Option.");
- }
- }
- void encipher()
- {
- unsigned int i,j;
- char input[257],key[33];
- printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:\n ");
- gets(input);
- printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: ");
- gets(key);
- for(i=0,j=0;i<strlen(input);i++,j++)
- {
- //repeat the key if you are at end of it.
- if(j>=strlen(key))
- {
- j=0;
- }
- //actual logic -> character from input + character from key % 26 is encrypted charater
- printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
- }
- }
- void decipher()
- {
- unsigned int i,j;
- char input[257],key[33];
- int value;
- printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:\n ");
- gets(input);
- printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
- gets(key);
- for(i=0,j=0;i<strlen(input);i++,j++)
- {
- //repeat the key if you are at end of it.
- if(j>=strlen(key))
- {
- j=0;
- }
- //similar to encipher only difference is you need to subtract
- value = (toupper(input[i])-64)-(toupper(key[j])-64);
- //if value is negative. We have to rotate it backwards (like backwards from z,y,x)
- //so add it to 26 (it's a negative value to adding will actually cause subtraction) to get original character.
- if( value < 0)
- {
- value = 26 + value;
- }
- printf("%c",65 + (value % 26));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement