-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel2.cpp
More file actions
69 lines (59 loc) · 1.29 KB
/
level2.cpp
File metadata and controls
69 lines (59 loc) · 1.29 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
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
pair<int,int> primefactors(int n){
int p,q;
if(n % 2 == 0){
p = 2;
q = n / p;
return make_pair(p,q);
}
else{
for(int i = 3; i <= sqrt(n) + 1; i+= 2){
if(n % i == 0){
p = i;
q = n / p;
return make_pair(p,q);
}
}
}
return make_pair(1,n);
}
int eulertotient(int p, int q){
return (p-1)*(q-1);
}
int privatekey(int e, int phi){
int n = 0;
while((phi*n + 1)%e != 0){
n++;
}
return (phi*n + 1)/e;
}
long long decrypt(long long C, int d, int n){
long long result = 1;
while(d > 0){
if(d % 2 == 1){
result = (result * C)%n;
}
d = d / 2;
C = (C * C)%n;
}
return result;
}
int main(){
int n = 421649;
int e = 17;
int p = primefactors(n).first;
int q = primefactors(n).second;
int phi = eulertotient(p,q);
int d = privatekey(e,phi);
int encryptedmsg[] = {101945, 148510, 150222, 81384, 363199, 199550, 308279, 148634};
int decryptedmsg[8];
for(int i = 0; i < 8; i++){
decryptedmsg[i] = decrypt(encryptedmsg[i], d, n);
}
for(int i: decryptedmsg){
cout<<i<<" ";
}
}