-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.cpp
More file actions
168 lines (103 loc) · 3.37 KB
/
Copy pathmessage.cpp
File metadata and controls
168 lines (103 loc) · 3.37 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include "bigint.h"
#include "rsakey.h"
#include "rsaengine.h"
#include "message.h"
using namespace std;
Message::Message(const char* msj){
text = new char[strlen(msj) + 1];
strcpy(text, msj);
blocks = nullptr; // aici e nul pana cand apelez toBlocks(), imi va initializa cu conversia corecta
blockCount = 0;
isEncrypted = false; //aici asum ca initial nu e criptat, destul de straightforward
}
Message::Message(const Message& msj) {
this->blockCount = msj.blockCount;
this->isEncrypted = msj.isEncrypted;
text = new char[strlen(msj.text) + 1];
strcpy(this->text, msj.text);
if (blockCount > 0) {
//am adaugat un safeguard fata de commitul anterior, verific blockcount ul
blocks = new BigInt[blockCount];
memcpy(blocks, msj.blocks, blockCount * sizeof(BigInt));
} else {
blocks = nullptr;
}
}
Message& Message::operator=(const Message& msj){
if (this == &msj) return *this;
delete[] text;
text = new char[strlen(msj.text) + 1];
strcpy(text, msj.text);
delete[] blocks;
blockCount = msj.blockCount;
isEncrypted = msj.isEncrypted;
if (blockCount > 0) {
blocks = new BigInt[blockCount];
memcpy(this->blocks, msj.blocks, blockCount* sizeof(BigInt));
// nu cred ca am zis niciodata in codebase, dar e echivalent cu :
// for (int i = 0; i < blockCount; i++)
// blocks[i] = msj.blocks[i];
} else {
blocks = nullptr;
}
return *this;
}
void Message::toBlocks( ){
if (blocks) delete[] blocks; //eliberez inainte de a realoca
blockCount = strlen(this->getText());
blocks = new BigInt[blockCount];
for (int i = 0 ; i<blockCount; i++){
blocks[i] = BigInt((uint64_t) text[i]); //castez ca sa se potr pe constructoru de la BIGINT
}
}
void Message::fromBlocks() {
if (!blocks || blockCount == 0) return ;
if (text) delete[] text;
text= new char[blockCount+1];
for (int i = 0 ; i<blockCount; i++){
text[i] = (char)blocks[i].getLimb(0); //imi ia pe least significant limb 64 biti si mi l casteaza in char
}
text[blockCount] = '\0';
isEncrypted = false; //cu metoda asta o sa reconstruiesc text din blocuri decriptate, automat nu vor mai fi criptate in acest punct al programului
}
int Message::getBlockCount() const{
return this->blockCount;
}
bool Message::getIsEncrypted() const {
return this->isEncrypted;
}
BigInt* Message::getBlocks() const{
return this->blocks;
}
const char* Message::getText() const{
return this->text;
}
void Message::setEncrypted(bool val) {
this->isEncrypted = val;
}
ostream& operator<<(ostream& os, const Message& msj) {
if(msj.isEncrypted) {
os<< "Encrypted message ( " << msj.blockCount << " blocks) :"<< endl;
for (int i =0; i< msj.blockCount ; i++){
os<<msj.blocks[i]<<" ";
}
} else{
os<<"Message: " << msj.text;
}
return os;
}
istream& operator>>(istream& is, Message& msj){
string input;
cout<< "Message: ";
is>> input;
delete[] msj.text;
msj.text= new char[input.size() + 1 ];
strcpy( msj.text, input.c_str());
msj.isEncrypted = false;
return is;
}
Message::~Message(){
if (text) delete[] text;
if ( blocks) delete[] blocks;
}