-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.cpp
68 lines (52 loc) · 1.19 KB
/
RSA.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
#include<bits/stdc++.h>
using namespace std;
typedef struct RSA_Crypto{
double p;
double q;
double e;
double phi;
double d;
}rsa;
int gcd(int a,int b){
if(a==0) return b;
return gcd(b%a, a);
}
rsa cryptor;
void init(){
cryptor = {3, 7, 2};
cryptor.phi = (cryptor.p - 1) * (cryptor.q - 1);
while(cryptor.e < cryptor.phi){
if(gcd(cryptor.e, cryptor.phi) == 1) break;
else cryptor.e++;
}
int k = 2;
cryptor.d = (1 + (k*cryptor.phi))/cryptor.e;
}
double RSAEncrypt(double message){
int k = 2;
double n = cryptor.p * cryptor.q;
double cipherText = pow(message, cryptor.e);
cipherText = fmod(cipherText, n);
return cipherText;
}
double RSADecrypt(double encryptedMessage){
double n = cryptor.p * cryptor.q;
double plainText = pow(encryptedMessage, cryptor.d);
plainText = fmod(plainText, n);
return plainText;
}
int main(){
init();
double message = 17;
cout<<"Message: "<<message<<endl;
double encryptedMessage = RSAEncrypt(message);
cout<<"Encrypted message: "<<encryptedMessage<<endl;
double decryptedMessage = RSADecrypt(encryptedMessage);
if(decryptedMessage == message){
cout<<"Decryption successful!!"<<endl;
}
else{
cout<<"Decryption failed!!"<<endl;
}
return 0;
}