-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.c
More file actions
130 lines (120 loc) · 4.97 KB
/
caesar.c
File metadata and controls
130 lines (120 loc) · 4.97 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
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// Counts the number of arguements in the command line
if (argc == 2)
{
// Converts index 1 of argv to an int
const int KEY = atoi(argv[1]);
// Stores a boolean to use as an off switch if we dectect a bad input
bool isKeyValid = true;
// Stores the length of the the string in index 1 of the argv array
int len = strlen(argv[1]);
// Loop that checks each digit to see if it's a number
for (int i = 0; i < len; i++)
{
// If isdigit detects a non-digit it'll set our stored bool to false and end the loop
if (isdigit(argv[1][i]) == false)
{
isKeyValid = false;
i = len;
}
}
// If the loop successfully finishes and the isKeyValid bool stays true then we have a valid key and can proceed
if (isKeyValid)
{
string plain = get_string("plaintext: ");
int plainLength = strlen(plain);
for (int i = 0; i < plainLength; i++)
{
if (isupper(plain[i]))
{
if (plain[i] + KEY > 'Z')
{
int keyRemainder = (plain[i] + KEY) - 'Z';
if (keyRemainder > 'Z' - 'A')
{
while (keyRemainder >= ('Z' - 'A'))
{
keyRemainder = keyRemainder - ('Z' - 'A');
}
if (plain[i] + keyRemainder > 'Z')
{
keyRemainder = plain[i] + keyRemainder - 'Z';
plain[i] = 'A' + keyRemainder - 1;
}
else
{
plain[i] = 'a' + keyRemainder - 1;
}
}
else
{
plain[i] = 'A' + keyRemainder - 1;
}
}
else if (plain[i] + KEY <= 'Z')
{
plain[i] = plain[i] + KEY;
}
}
if (islower(plain[i]))
{
if (plain[i] + KEY > 'z')
{
// Takes the value of our selected letter and adds total steps then subtracts 'z' to take it down a whole rotation of the wheel
int keyRemainder = (plain[i] + KEY) - ('z');
// Checks to see if the left over amount of steps is still greater than the value of the entire length of the alphabet
if (keyRemainder >= 'z' - 'a')
{
// While the keyRemainder is greater than 25 we continue this loop
while (keyRemainder >= ('z' - 'a'))
{
// Each iteration subtracts 26 "length of the alphabet" from the total
keyRemainder = keyRemainder - (26);
}
// If the initial letter value + the new remainder is still greater than z, we do one last wrap a round by taking the difference and adding it to the value of 'a'.
if (plain[i] + keyRemainder > 'z')
{
keyRemainder = plain[i] + keyRemainder - 'z';
plain[i] = 'a' + keyRemainder - 1;
}
// If the keyRemainder + the initial letter value is not greater than 'z' than we take the initial letter value and add the number of steps left in the keyRemainder
else
{
plain[i] = 'a' + keyRemainder - 1;
}
}
else
{
plain[i] = 'a' + keyRemainder - 1;
}
}
else if (plain[i] + KEY <= 'z')
{
plain[i] = plain[i] + KEY;
}
}
}
printf("ciphertext: %s\n", plain);
}
// If we detected an unusable key we ask for a proper key
else
{
printf("Usage: ./caesar key\n");
printf("1");
return (1);
}
}
// If too many arguements were entered then we ask for a proper key
else if (argc != 2)
{
printf("Usage: ./caesar key\n");
printf("1");
return (1);
}
}