-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviginere.c
More file actions
135 lines (116 loc) · 2.98 KB
/
viginere.c
File metadata and controls
135 lines (116 loc) · 2.98 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* viginere.c - implements viginere cipher */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char *keygen(char*, char*); // generates a key for the plaintext
char caesar(char, int); // shifts a character by a shift amount
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Must supply a key!\n");
return 1;
}
char *oKey = argv[1];
// check for any non alpha in rKey
int n;
int kLen = strlen(oKey);
for (n = 0 ; n < kLen; n++)
{
if (!isalpha(oKey[n]))
{
printf("Invalid character in key!\n");
return 1;
}
}
// grab plaintext from user (length up to 512 bytes)
printf("Please enter plaintext: ");
char p[512];
fgets(p, 512, stdin);
char* rKey = keygen(p, oKey);
int i;
int pLen = strlen(p);
int shift;
char *result = calloc(strlen(p) , sizeof(char));
for (i=0; i<pLen; i++)
{
if (isalpha(p[i]))
{
// determine shift from rKey by subtracing the ascii value of the beginning of the alphabet
if (isupper(rKey[i]))
{
shift = rKey[i] - 'A';
}
else
{
shift = rKey[i] - 'a';
}
char res = caesar(p[i], shift);
//printf("shifting %c by %d: %c\n", p[i], shift, res);
result[i] = res;
}
else
{
result[i] = p[i];
}
}
printf("%s", p);
//printf("%s\n", rKey);
printf("%s", result);
return 0;
}
char *keygen(char* p, char* oKey)
{
char *rKey = calloc(strlen(p), sizeof(char)); // allocate enough memory to hold the repeated key
int oKeyLen = strlen(oKey);
int pLen = strlen(p);
int i = 0;
int j = 0;
int pos = 0;
while (i<pLen)
{
if (!isalpha(p[i]))
rKey[i] = ' '; // space in rKey at i if not a letter
else
{
if (i > 0)
pos = j % oKeyLen; // determine which part of okey to use based on a position irrespective of spaces in p
if (isupper(p[i]))
rKey[i] = toupper(oKey[pos]); // print upper char
else
rKey[i] = tolower(oKey[pos]); // lower char
j++; // increment position in oKey
}
i++; // increment position in p
}
rKey[i] = '\0'; // cant forget the allmighty null-terminator
return rKey;
}
char caesar(char c, int shift)
{
if (isupper(c))
{
// would it overrun for uppercase?
if (c + shift > 'Z')
{
return 'A' + (c + shift) % 'Z' - 1; // print the wrap around
}
else
{
return c + shift;
}
}
else
{
// would it overrun for lowercase?
if (c + shift > 'z')
{
return 'a' + (c + shift) % 'z' - 1;
}
else
{
return c + shift;
}
}
}