-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsakey.cpp
More file actions
155 lines (115 loc) · 4 KB
/
Copy pathrsakey.cpp
File metadata and controls
155 lines (115 loc) · 4 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
#include "rsakey.h"
const BigInt RSAKey::e = BigInt(65537);
int RSAKey::keyCount = 0;
RSAKey::RSAKey(const BigInt& p, const BigInt& q, const char* ownerName) : bitSize(p.bitLength() + q.bitLength()) {
this->p = p;
this->q = q;
n = p * q;
owner = new char[strlen(ownerName) + 1];
strcpy(owner, ownerName);
keyCount++;
}
RSAKey::RSAKey(const RSAKey& obj) : bitSize(obj.bitSize) { // copy constructorul
this->p = obj.p;
this->q = obj.q;
this->n = obj.n;
this->d = obj.d;
owner = new char[strlen(obj.owner) + 1];
strcpy(owner, obj.owner);
keyCount++;
}
RSAKey& RSAKey::operator=(const RSAKey& obj) {
if (this == &obj) return *this;
this->p = obj.p;
this->q = obj.q;
this->n = obj.n;
this->d = obj.d;
this->bitSize = obj.bitSize;
delete[] owner; // obiectul daca nu l eliberez acm mi s ar elibera memoria de abia la final de lifetime
owner = new char[strlen(obj.owner) + 1];
strcpy(owner, obj.owner);
return *this;
}
RSAKey::~RSAKey() { if (owner) delete[] owner; }
BigInt RSAKey::modInv(const BigInt& a, const BigInt& m) {
// euclid extins: gasesc x a.i. a * x congruent 1 (mod m)
// tin semnul separat pt ca BigInt e unsigned si algoritmul produce coeficienti negativi
BigInt rest_vechi = a, rest = m;
BigInt coef_vechi(1), coef(0);
bool coef_vechi_negativ = false, coef_negativ = false;
while (!rest.isZero()) {
BigInt cat = rest_vechi / rest;
// actualizez restul: rest_nou = rest_vechi - cat * rest
BigInt temp_rest = rest;
rest = rest_vechi - cat * rest;
rest_vechi = temp_rest;
// actualizez coeficientul: coef_nou = coef_vechi - cat * coef
// tin semnul separat ca sa nu am underflow pe unsigned
BigInt cat_coef = cat * coef;
BigInt temp_coef = coef;
bool temp_negativ = coef_negativ;
if (coef_vechi_negativ == coef_negativ) {
// acelasi semn => scadere
if (coef_vechi >= cat_coef) {
coef = coef_vechi - cat_coef;
coef_negativ = coef_vechi_negativ;
} else {
coef = cat_coef - coef_vechi;
coef_negativ = !coef_vechi_negativ; // se inverseaza semnul
}
} else {
// semne diferite => adunare (minus cu minus da plus)
coef = coef_vechi + cat_coef;
coef_negativ = coef_vechi_negativ;
}
coef_vechi = temp_coef;
coef_vechi_negativ = temp_negativ;
}
// daca coeficientul e negativ, il aduc in pozitiv adaugand modulul
if (coef_vechi_negativ)
coef_vechi = m - coef_vechi;
return coef_vechi;
}
void RSAKey::generatePrivateKey() {
n = p*q;
BigInt phi = (p - BigInt(1)) * (q - BigInt(1));
d = modInv(e,phi);
}
ostream& operator<<(ostream& os, const RSAKey& key) {
os << "Owner: " << key.owner << endl;
os << "n: " << key.n << endl;
os << "e: " << key.e << endl;
os << "d: " << key.d << endl;
os << "Bit size: " << key.bitSize << endl;
os << "Key #" << key.keyCount << endl; //asta poate nu e la fel de clar, imi printeaza nr de ordine al cheii
return os;
}
istream& operator>>(istream& is, RSAKey& key) {
string ownerName;
cout << "Owner: ";
is >> ownerName;
delete[] key.owner; //eliberez mem alocata in constructorul cu parametri, ca sa o realoc cu alt owner
key.owner = new char[ownerName.size() + 1];
strcpy(key.owner, ownerName.c_str()); //aici il fac const char* ptr ca asa accepta strcpy ca source
return is;
}
const BigInt& RSAKey::getN() const {
return this->n;
}
const BigInt& RSAKey::getD() const {
return this->d;
}
const BigInt& RSAKey::getE() {
return e;
}
const char* RSAKey::getOwner() const {
return owner;
}
void RSAKey::setOwner(const char* newOwner) {
delete[] this->owner;
owner = new char[strlen(newOwner)+1];
strcpy(owner, newOwner);
}
int RSAKey::getBitSize() const {
return bitSize;
}