-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.c
93 lines (78 loc) · 2.2 KB
/
vigenere.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
#include<string.h>
int main(){
char text[] = "THECRAZYPROGRAMMER";
char key[] = "HELLO";
int textLength = strlen(text);
int keyLength = strlen(key);
int i, j; //counters
char newKey[textLength];
char encryptedText[textLength];
char decryptedText[textLength];
int option;
printf("Vigenere Cipher\n\nPlease choose from following options""\n1. Encrypt\n2. Decrypt\nYour Option: ");
scanf("%d", &option);
switch(option) {
case 1:
printf("Enter text to encrypt: ");
scanf("%s", &text);
printf("Enter Key: ");
scanf("%d", &key);
//Create new key equal to size of text
for(i = 0, j = 0; i < textLength; ++i, ++j){
if(j == keyLength)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
//encryption
for(i = 0; i < textLength; ++i){
encryptedText[i] = ((text[i] + newKey[i]) % 26) + 'A';
}
encryptedText[i] = '\0';
printf("Encrypted Text: %s", encryptedText);
break;
case 2:
printf("Enter text to decrypt: ");
scanf("%s", &encryptedText);
printf("Enter Key: ");
scanf("%d", &key);
//Create new key equal to size of text
for(i = 0, j = 0; i < textLength; ++i, ++j){
if(j == keyLength)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
//Decryption
for(i = 0; i < textLength; ++i){
decryptedText[i] = (((encryptedText[i] - newKey[i]) + 26) % 26) + 'A';
}
decryptedText[i] = '\0';
printf("\nDecrypted Message: %s", decryptedText);
break;
default:
printf("Wrong Option Selected");
}
return 0;
//Check if the Text is alphabetical
for (i=0;i<textLength;i++){
if(!isalpha(text[i])){
printf("You have non-alphabetical chars in Text \n");
return 1;
}
}
//Check if key is alphabetical
for (i=0;i<keyLength;i++){
if(!isalpha(key[i])){
printf("You have non-alphabetical chars in Key \n");
return 1;
}
}
//decryption
printf("Original Message: %s", text);
printf("\nKey: %s", key);
printf("\nNew Generated Key: %s", newKey);
printf("\nEncrypted Message: %s", encryptedText);
return 0;
}