-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvigenere.c
126 lines (102 loc) · 2.68 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
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
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/**
* This program implements Vigenere's cipher which improves upon
* Caesar's cipher, by encrypting messages using a sequence of keys,
* from a keyword.
*
* USAGE:
* $./vigenere <input string keyword i.e. non-numeric and special characters>
* plaintext: <input text>
* ciphertext: <outputs encrypted text>
*
**/
// Declare functions here
int get_ascii_offset(char c);
bool is_key_valid(string key);
char encipher_char(char c, int k);
// BEGIN MAIN
int main(int argc, string argv[])
{
// 1. Get the keyword
// a) Validate number of CLI arguments
if (argc != 2)
{
printf("Usage: ./vigenere <keyword>\n");
return 1;
}
// b) Check if keyword is valid non-numeric alphabet keyword.
if (!is_key_valid(argv[1]))
{
printf("Keyword has to be non-numeric alphabet keyword.\n");
return 1;
}
// Store keyword in var keyword
string keyword = argv[1];
// 2. Get the plain text
string plain_text = get_string("plaintext: ");
printf("ciphertext: ");
int j = 0;
int keyword_len = strlen(keyword);
// 3. Encipher
for (int i = 0, n = strlen(plain_text); i < n; i++)
{
// a) Check if char is alphabet character or not
// If not than continue
if (!isalpha(plain_text[i]))
{
printf("%c", plain_text[i]);
continue;
}
// b) Encipher the plain_text char
char key_char = keyword[j++ % keyword_len];
int ascii_offset = get_ascii_offset(key_char);
int key = key_char - ascii_offset;
char c = encipher_char(plain_text[i], key);
printf("%c", c);
}
printf("\n"); // Adds new line
return 0; // All went well, return success
}
// END MAIN
/**
* Function encrypts ASCII char, based on the integer key provided.
* INPUT: char c, int k
* OUPUT: return encrypted char c
**/
char encipher_char(char c, int k)
{
int ascii_offset = get_ascii_offset(c);
int offset_char = c - ascii_offset;
offset_char = (offset_char + k) % 26;
c = (char) offset_char + ascii_offset;
return c;
}
/**
* Function cheks whether string key is valid keyword or not.
* INPUT: string keyword
* OUTPUT: true or false
**/
bool is_key_valid(string keyword)
{
for (int i = 0, n = strlen(keyword); i < n; i++)
{
if (!isalpha(keyword[i]))
{
return false;
}
}
return true;
}
/**
* Function returns ascii offset for upper or lower case character
* INPUT: char c
* OUTPUT: 65 for UPPER, and 97 for lower
**/
int get_ascii_offset(char c)
{
return isupper(c) ? 65 : 97;
}