-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.c
91 lines (79 loc) · 2.76 KB
/
vigenere.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
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
//Check number of parameters
if (argc != 2)
{
printf("! Try again.\n");
return 1;
}
//loop through the key to check if it is alphabetical
int argv_length = strlen(argv[1]);
for (int i = 0; i < argv_length; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Wrong input! Try again.\n");
return 1;
}
}
//GetTing the user's string
printf("plaintext:");
string input_text = GetString();
//Declare variables and setting them to '0'
int i = 0;
int modulo = 0;
int input_length = 0;
int key_index = 0;
string key = argv[1];
printf("ciphertext:");
//Loop through user's input
for (i = 0, input_length = strlen(input_text); i < input_length; i++)
{
//Checking if user's input is alphabetical, then make cipher
if (isalpha(input_text[i]))
{
key_index = modulo % strlen(key);
//Get cipher of thr input text using formula ci = (pi + kj) % 26
//depending on letter case and according to the positions of letters in ASCII table
if (islower(input_text[i]))
{
if (islower(key[key_index]))
{
int lower_output = ((input_text[i] - 'a' + key[key_index] - 'a') % 26) + 'a';
printf("%c", lower_output);
}
else
{
int lower_output = ((input_text[i] - 'a' + key[key_index] - 'A') % 26) + 'a';
printf("%c", lower_output);
}
}
if (isupper(input_text[i]))
{
if (islower(key[key_index]))
{
int upper_output = ((input_text[i] - 'A' + key[key_index] - 'a') % 26) + 'A';
printf("%c", upper_output);
}
else
{
int upper_output = ((input_text[i] - 'A' + key[key_index] - 'A') % 26) + 'A';
printf("%c", upper_output);
}
}
//increment key position (modulo) sto use next letter in key
modulo++;
}
else
{
//if input is not alphabetical output chars as they are
printf("%c", input_text[i]);
}
}
printf("\n");
}