-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
126 lines (100 loc) · 4.1 KB
/
functions.cpp
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 <cctype>
#include <iostream>
#include "functions.h"
using namespace std;
//function to find the shift value based of a letter
int getShift(char inputLetter) {
if (isdigit(inputLetter)) {
return inputLetter - '0'; //if the key value is a digit, we will just return that as shift value. ascii of '0' is subtracted from digit to get the actual number
} else {
char input = tolower(inputLetter); //convert to lowercase
int shiftVal = int(input)-'a'; //convert to ascii value and subtract to get positional value of letter
return shiftVal;
}
}
//given a letter and a number to shift by, find the new resulting letter
char mapShift(char inputLetter, int shift) {
char input = tolower(inputLetter);
char lowerShift = tolower(shift);
int inputPos = getShift(input);
int outputPos = (inputPos + lowerShift) % 26;
char outputLetter = char(outputPos + 97);
return outputLetter;
}
//do reverse
char reverseShift(char inputLetter, int shift) {
if (isupper(inputLetter)) {
int inputPos = inputLetter - 'A'; //ascii of capital A is 65
int outputPos = (inputPos - shift + 26) % 26; // +26 to handle negative values, which otherwise enter ascii range of capital letters
return 'A' + outputPos;
} else {
int inputPos = inputLetter - 'a'; //67
int outputPos = (inputPos - shift + 26) % 26;
return 'a' + outputPos; //regardless of input case we will get output of lower
}
}
//testing function
void testFunctions(){
cout << "==TESTING FUNCTIONS OUTPUT==" << endl;
int shift = getShift('A');
cout << "ShiftVal for A: " << shift << endl;
int shift2 = getShift('B');
cout << "ShiftVal for B: " << shift2 << endl;
int shift3 = getShift('5');
cout << "ShiftVal for 5: " << shift3 << endl;
cout << "Mapping A with shift of 1: " << mapShift('A', 1) << endl;
cout << "Mapping B with shift of 2: " << mapShift('B', 2) << endl;
cout << "Mapping Z with shift of 1: " << mapShift('Z', 1) << endl;
cout << "Mapping Z with shift of 2: " << mapShift('Z', 2) << endl;
};
//the above are useful for caesar ciphers, but in the main function we will be using these for vigenere ciphers, which are a type of caesar cipher
string filterKey(string key) {
string filteredKey = "";
for (char c : key) {
if (isalpha(c) || isdigit(c)) {
filteredKey += c;
}
}
return filteredKey;
}
//main vigenere function
string vigenereEncrypt(string inputMessage, string key) {
string filteredKey = filterKey(key);
int keyLength = filteredKey.length();
string outputMessage = "";
int keyPos = 0;
for (char c : inputMessage) {
if (isalpha(c)) {
int shiftVal = getShift(filteredKey[keyPos % keyLength]); //modulo wraps around the key
char shiftedChar = mapShift(c, shiftVal);
cout << "KEY: Shift value for char " << filteredKey[keyPos % keyLength] << " is " << shiftVal << endl;
cout << "TEXT: Shifting " << c << " by " << shiftVal << ". End result is: " << shiftedChar << endl;
outputMessage += shiftedChar;
keyPos += 1;
}
else {
outputMessage += c; //nonalpha characters are preserved
}
}
return outputMessage;
}
string vigenereDecrypt(string inputMessage, string key) {
string filteredKey = filterKey(key);
int keyLength = filteredKey.length();
string outputMessage = "";
int keyPos = 0;
for (char c : inputMessage) {
if (isalpha(c)) {
int shiftVal = getShift(filteredKey[keyPos % keyLength]); //modulo wraps around the key
char shiftedChar = reverseShift(c, shiftVal);
cout << "KEY: Shift value for char " << filteredKey[keyPos % keyLength] << " is " << shiftVal << endl;
cout << "TEXT: Shifting " << c << " by " << shiftVal << ". End result is: " << shiftedChar << endl;
outputMessage += shiftedChar;
keyPos += 1;
}
else {
outputMessage += c; //nonalpha characters are preserved
}
}
return outputMessage;
}