-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.c
More file actions
84 lines (69 loc) · 2 KB
/
encrypt.c
File metadata and controls
84 lines (69 loc) · 2 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
/**********************
* Alexei Beizerov *
* serothim@gmail.com *
*skype name: serothim *
**********************/
#include "vigenere_cipher.h"
static int shift(const char* const keyword);
/*
* This feature encrypts incoming text.
* Accepts two parameters: a keyword and plaintext for encryption.
* The function returns a pointer to the cipher text.
*/
string encrypt(const char* const keyword, char* const str)
{
const int LETTER_COUNT = 26;
if (str != NULL)
{
int str_length = strlen(str);
// Variable value_to_increase to store the value by which
// the value of the character is increased during encryption.
int value_to_increase = 0;
// Changes plaintext characters to encrypted
for (int i = 0; i < str_length; i++)
{
if (isalpha(str[i]))
{
// Shift is used only if the character is an alphabet character.
value_to_increase = shift(keyword);
if (isupper(str[i]) && (str[i] + value_to_increase) > 'Z')
{
str[i] += value_to_increase - LETTER_COUNT;
}
else if (islower(str[i]) && (str[i] + value_to_increase) > 'z')
{
str[i] += value_to_increase - LETTER_COUNT;
}
else
{
str[i] += value_to_increase;
}
}
}
return str;
}
return NULL;
}
/*
* This function shifts to the next character in the keyword
* and return a value from 0 to 25, like from A to Z.
*
* A == 0, Z == 25.
* Letter case not have matter.
*/
static int shift(const char* const keyword)
{
static int j = 0;
if (keyword[j] == '\0')
{
j = 0; // Set up the index into the beginning character of the keyword
}
if (isupper(keyword[j]))
{
return keyword[j++] - 'A';
}
else
{
return keyword[j++] - 'a';
}
}