-
Notifications
You must be signed in to change notification settings - Fork 1
/
substitution.c
145 lines (134 loc) · 3.42 KB
/
substitution.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
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
136
137
138
139
140
141
142
143
144
145
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool validateKey(string s);
bool createChiperText(string plain, string key, char *result);
int main(int argc, char *argv[])
{
//if there is not argument given return with usage hint
if (argc < 2 || argc > 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
string Key = argv[1];
if (!validateKey(Key))
{
printf("Key must contain 26 unique characters.\n");
return 1;
}
string plain = get_string("plaintext: ");
string chiper = "";
//create chiper text
if (createChiperText(plain, Key, chiper))
{
return 0;
}
else
{
return 1;
}
}
//validates the key, checks length and occurance of letters
bool validateKey(string s)
{
if (strlen(s) < 26 || strlen(s) > 26)
{
return false;
}
//creating buffer to count the chars, also set all elements to 0
int arr[26] = {0};
//check every char in string
for (int idx = 0; idx < strlen(s); idx++)
{
//if not a valid alphanumeric char return false
if (!isalpha(s[idx]))
{
return false;
}
else
{
int index = 0;
//calculating index of letter
if (islower(s[idx]))
{
index = ((int)s[idx]) - 97;
}
else
{
index = ((int)s[idx]) - 65;
}
//check if the index fits to the array
if (index >= 0 && index < 26)
{
//index already existent?
arr[index]++;
if (arr[index] > 1)
{
return false;
}
}
else
{
return false;
}
}
}
return true;
}
//create the chiper text
bool createChiperText(string plain, string key, char *result)
{
int length = strlen(plain);
//creating buffer and allocating memory
char *buff = 0;
buff = malloc(sizeof(buff) * length);
//iterate through every plain char
for (int idx = 0; idx < strlen(plain); idx++)
{
int index = 0;
bool lowerd = false;
//calculating index of letter
if (islower(plain[idx]))
{
index = ((int)plain[idx]) - 97;
lowerd = true;
}
else
{
index = ((int)plain[idx]) - 65;
}
//check if the index fits to the array
if (index >= 0 && index < 26)
{
char chipher_char = key[index];
//check if we need to convert to upper or lower
if (islower(chipher_char) && !lowerd)
{
chipher_char = toupper(chipher_char);
}
else if (!islower(chipher_char) && lowerd)
{
chipher_char = tolower(chipher_char);
}
//if we got a char, add to buffer
if (chipher_char)
{
strncat(buff, &chipher_char, 1);
}
}
else //adding chars which do not fit to our key
{
strncat(buff, &plain[idx], 1);
}
}
if (!sizeof(buff)) // if we could not create a chipher text return false
{
return false;
}
//all good. printing and return true
printf("ciphertext: %s\n", buff);
return true;
}