-
Notifications
You must be signed in to change notification settings - Fork 0
/
code1.c
100 lines (94 loc) · 2.97 KB
/
code1.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "cs50.h"
#define STRLEN_ALLOCATED_IN_SWAP 8192
char substitute_char(char c, string k);
string perform_substitution(string s, string k);
int main(int argc, string argv[])
{
// Exit if too many arguments or too few
if (argc != 2)
{
printf("Usage: ./substitution <key>\n");
return 1;
}
// Exit if key length is not 26
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters\n");
return 1;
}
string key = argv[1];
// Check that all characters in the key are letters
char letters_used_in_key[26] = " ";
for (int i = 0, n = strlen(key); i < n; i++)
{
char this_char = key[i];
if (!isalpha(this_char))
{
printf("Key must be comprised of letters!\n");
return 1;
}
if (strchr(letters_used_in_key, this_char) != NULL)
{
// Check that all characters are only used once
// I don't need to check that they're all used once, only that none are used twice because there are 26 possibilities and 26 slots.
printf("Only one of each letter is allowed! (You used %c twice!)\n", this_char);
return 1;
}
letters_used_in_key[i] = this_char;
}
// Get plaintext
string plaintext = get_string("plaintext: ");
if (strlen(plaintext) > STRLEN_ALLOCATED_IN_SWAP)
{
printf("Your plaintext is too long!\n");
return 1;
}
// Encrypt
char *result = perform_substitution(plaintext, key);
// Present result
printf("ciphertext: ");
for (int i = 0, n = strlen(result); i < n; i++)
{
printf("%c", result[i]);
}
printf("\n");
}
// Substitute a single character
char substitute_char(char c, string k)
{
if (isupper(c))
{
// If the character is uppercase, return the key index of that character:
// The "- 'A'" gives the right index, because if the character is 'A', it would be the first character in the alphabet and therefore should the get the first character in the key.
// 'A' - 'A' = 0!
return toupper(k[c - 'A']);
}
else
{
// The same thing, but with lowercase characters:
// 'a' - 'a' = 0.
return tolower(k[c - 'a']);
}
}
// k is the key, s is the input
string perform_substitution(string s, string k)
{
// Just call "substitute_char" on every char in the string.
char *output = (char *)malloc(sizeof(char) * STRLEN_ALLOCATED_IN_SWAP);
output[0] = '\0';
for (int i = 0, n = strlen(s); i < n; i++)
{
char this_char = s[i];
char to_append = this_char;
if (isalpha(this_char))
{
to_append = substitute_char(this_char, k);
}
strncat(output, &to_append, 1);
}
return output;
}