-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vernam Cipher.cpp
101 lines (87 loc) · 2.75 KB
/
Vernam Cipher.cpp
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
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
char cptext[100];
void Input(char [], char [] );
void encrypt(char [], char [] );
void decrypt(char [], char [] );
void prePosses (char [], char [] );
void vernamCipher (char [], char [] );
int main ()
{
char str[100], key[100];
printf("\tVernam Cipher Algorithm\n");
printf("\t-----------------------\n");
vernamCipher(str, key); /* algorithm */
return 0;
}
void vernamCipher (char str[], char key[]) {
int choice;
while(1) {
printf("\n\t\t(1) Encrypt");
printf("\n\t\t(2) Decrypt");
printf("\n\t\t(3) Exit");
printf("\n\t\tEnter your choice: "); scanf("%d", &choice);
getchar();
switch(choice) {
case 1:
Input(str, key);
prePosses(str, key);
encrypt(str, key); printf("\n\t\tEncrypt text: %s\n", cptext);
break;
case 2:
Input(str, key);
prePosses(str, key);
decrypt(str, key); printf("\n\t\tDecrypt text: %s\n", cptext);
break;
default:
exit(0);
break;
}
}
}
void Input(char str[], char key[]) {
printf("\n\t\tEnter plain text: "); gets(str);
printf("\t\tEnter key: "); scanf("%s", key);
}
void encrypt(char str[], char key[]) {
int in = 0;
for(int i=0; str[i]; i++) {
if (str[i]>='a' && str[i]<='z') {
char ch = (key[i]>='a' && key[i]<='z') ? 'a' : 'A';
int ind = ((str[i]-'a' + key[i] - ch) % 26) + 'a';
cptext[in++] = ind;
} else if (str[i]>='A' && str[i]<='Z') {
char ch = (key[i]>='a' && key[i]<='z') ? 'a' : 'A';
int ind = ((str[i]-'A' + key[i] - ch) % 26) + 'A';
cptext[in++] = ind;
} else cptext[in++] = str[i];
}
cptext[in] = '\0';
}
void decrypt(char str[], char key[]) {
int in = 0;
for(int i=0; str[i]; i++) {
if (str[i]>='a' && str[i]<='z') {
char ch = (key[i]>='a' && key[i]<='z') ? 'a' : 'A';
int ind = ((str[i]-'a') - (key[i]-ch));
ind = (ind < 0) ? (ind + 26)+'a' : (ind + 'a');
cptext[in++] = ind;
} else if (str[i]>='A' && str[i]<='Z') {
char ch = (key[i]>='a' && key[i]<='z') ? 'a' : 'A';
int ind = ((str[i]-'A') - (key[i]-ch));
ind = (ind < 0) ? (ind + 26)+'A' : (ind + 'A');
cptext[in++] = ind;
} else cptext[in++] = str[i];
}
cptext[in] = '\0';
}
void prePosses (char str[], char key[]) {
int lenMsg = strlen(str);
int lenKey = strlen(key);
if (lenMsg != lenKey) {
for(int i=lenKey; i<lenMsg; i++) {
key[i] = key[lenKey-1];
}
key[lenMsg] = '\0';
}
}