/
binary.cpp
116 lines (109 loc) · 2.84 KB
/
binary.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
/**
Binary code C++ implementation.
@file binary.cpp
@author Devansh Singh
Chirag Tyagi
@brief Binary code is a cipher where the ascii code of characters of the
plaintext are converted to their binary equivalent.
@date 01/03/2022
*/
#include <bitset>
#include <vector>
#include "binary.hpp"
/**
Default constructor for the BinaryCode class.
*/
BinaryCode::BinaryCode() {
is_file = false;
}
/**
Parameterized constructor for the BinaryCode class.
@param data Data to encrypt/decrypt.
@param is_file Read/Write from a file. Defaults to false.
*/
BinaryCode::BinaryCode(std::string data, bool is_file) {
if(is_file) {
file_path = data;
this->is_file = true;
}else {
this->data = data;
this->is_file = false;
}
}
/**
Overload extraction (<<) operator for displaying data.
@param output Reference to output stream.
@param obj Reference to BinaryCode object.
@returns Reference to output stream.
*/
std::ostream& operator<<(std::ostream &output, BinaryCode &obj) {
output << obj.data;
return output;
}
/**
Overload insertion (>>) operator for taking data from user.
@param input Reference to input stream.
@param obj Reference to BinaryCode object.
@returns Reference to input stream.
*/
std::istream& operator>>(std::istream &input, BinaryCode &obj) {
std::getline(input, obj.data);
return input;
}
/**
Encrypt the plaintext.
@returns Encrypted ciphertext.
*/
std::string BinaryCode::encrypt() {
if(is_file) {
FileIO file(file_path);
data = file.read();
}
std::string result = "";
for(int index = 0; index < data.length(); index++) {
std::string binary = std::bitset<8>((int)data[index]).to_string();
result += binary;
if(index != data.length() - 1) {
result += std::string(" ");
}
}
data = result;
if(is_file) {
FileIO file(file_path);
return (file.write(data) == true ? "0" : "1");
}
return data;
}
/**
Decrypt the ciphertext.
@returns Decrypted plaintext.
*/
std::string BinaryCode::decrypt() {
if(is_file) {
FileIO file(file_path);
data = file.read();
}
std::vector<std::string> chars;
std::string current = "", result = "";
for(int index = 0; index < data.length(); index++) {
if(data[index] != ' ') {
current += data[index];
}else {
chars.push_back(current);
current = "";
}
}
if(current.length()) {
chars.push_back(current);
}
for(auto binary: chars) {
unsigned long decimal = std::bitset<8>(binary).to_ulong();
result += (char)decimal;
}
data = result;
if(is_file) {
FileIO file(file_path);
return (file.write(data) == true ? "0" : "1");
}
return data;
}