-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone_time_pad.cpp
45 lines (39 loc) · 1.2 KB
/
one_time_pad.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
#include <fstream>
#include "./one_time_pad.h"
const int MODULAR_SCALE = 'Z';
namespace one_time_pad{
std::string load_file(std::string path) {
std::ifstream file;
file.open(path);
std::string data = "";
getline(file, data);
return data;
}
std::string encrypt(std::string key, std::string plain_text){
std::string encrypted = "";
for(int i = 0; i < plain_text.size(); i++){
encrypted += modular_encode(key[i], plain_text[i]);
}
return encrypted;
}
std::string decrypt(std::string key, std::string encrypted){
std::string plain_text = "";
for(int i = 0; i < encrypted.size(); i++){
plain_text += modular_decode(encrypted[i], key[i]);
}
return plain_text;
}
char modular_encode(int key_value, int plain_value){
char sum = key_value + plain_value;
if(sum > MODULAR_SCALE){
sum -= MODULAR_SCALE;
}
return sum;
}
char modular_decode(int encoded_value, int key_value){
if(key_value > MODULAR_SCALE){
key_value -= MODULAR_SCALE;
}
return (char)(encoded_value - key_value);
}
}