-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.h
172 lines (145 loc) · 4.66 KB
/
RSA.h
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
169
170
171
172
//---------------------------------------------------------
// Main class for RSA de-/encprytion.
//
// Author: Sebastian Weninger
//
#ifndef RSA_
#define RSA_
#include <ostream>
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using std::string;
namespace Crypto
{
typedef std::vector<int256_t> CryptoString; // To store encrypted chars
typedef int256_t CryptoChar; // To store encrypted chars
// struct to hold private key members
typedef struct
{
int256_t s;
int256_t p;
int256_t q;
} PrivateKey;
// struct to hold public key members
typedef struct
{
int256_t r;
int256_t m;
} PublicKey;
class RSA
{
private:
// PRIVATE KEY MEMBERS
int256_t p;
int256_t q;
int256_t phi_of_m;
//PUBLIC KEY MEMBERS
int256_t m;
int256_t r;
PrivateKey* private_key;
PublicKey* public_key;
void calcPublicKey();
void calcPrivateKey();
int256_t makePositive(int256_t numb, int256_t mod) const;
//-----------------------------------------------------
// Set class members for calculating the private and
// public key.
//
// int256_t p: First prime number (has to be prime)
// int256_t q: Second prime number (has to be prime)
// int256_t r: public key member (gcd of 'r' and 'p * q' must be 1)
void setParameters(int256_t p, int256_t q, int256_t r);
//-----------------------------------------------------
// Calculate private and public key. (Only if class
// members are set previously)
//
void calcKeys();
//-----------------------------------------------------
// Checks if given number is a prime number.
//
// int256_t numb: number to check
// return: true if it is prime; false if not
static bool isPrime(int256_t numb);
//-----------------------------------------------------
// Calculate phi of two prime numbers
//
// int a: first prime number
// int b: second prime number
// return: phi of m
static int256_t calcPhi(int256_t a, int256_t b);
public:
RSA() = delete;
//-----------------------------------------------------
// Constructor
//
// int256_t p: First prime number
// int256_t q: Second prime number
// int256_t r: First public-key member
//
// Important: r must be coprime with phi of m!
// r also must be equal or less than phi of m!
// p and q must be prime numbers!
//
RSA(int256_t p, int256_t q, int256_t r);
//-----------------------------------------------------
// return: const pointer to public key
const PublicKey* getPublicKey() const;
//-----------------------------------------------------
// return: const pointer to private key
const PrivateKey* getPrivateKey() const;
//-----------------------------------------------------
// Encrypts a string
//
// str: string to be encrypted
//
// return: CryptoString; Structure which contains
// encrypted message
//
CryptoString encrypt(string str);
//-----------------------------------------------------
// Decrypts a string
//
// str: string to decrypt
//
// return: string; Decrypted message
//
string decrypt(CryptoString str);
//-----------------------------------------------------
// Encrypts a string
//
// ch: character to be encrypted
//
// return: CryptoChar; Boost's 256bit integer which contains
// the encrypted character
//
CryptoChar encrypt(char ch);
//-----------------------------------------------------
// Decrypts a string
//
// str: character to decrypt; Boost's 256bit integer which contains
// the decrypted character
//
// return: char; Encrypted character
//
char decrypt(CryptoChar str);
//-----------------------------------------------------
// "<<" operator overload for printing key members to std::out
friend std::ostream& operator <<(std::ostream& os, const RSA& rsa)
{
if ((rsa.public_key == nullptr) || (rsa.private_key == nullptr))
{
std::cout << "[ERROR] Can't print keys because one or both are null-pointers!\n";
}
std::cout << "---- PUBLIC KEY ----\n";
std::cout << "---- r = " << rsa.getPublicKey()->r << " ----\n";
std::cout << "---- m = " << rsa.getPublicKey()->m << " ----\n";
std::cout << "---- PRIVATE KEY ----\n";
std::cout << "---- s = " << rsa.getPrivateKey()->s << " ----\n";
std::cout << "---- p = " << rsa.getPrivateKey()->p << " ----\n";
std::cout << "---- q = " << rsa.getPrivateKey()->q << " ----\n";
return os;
}
};
}
#endif