Skip to content

Commit 5c2983c

Browse files
committed
Add project files.
1 parent ae15bbd commit 5c2983c

17 files changed

+1116
-0
lines changed

ChinRemainder.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "ChinRemainder.h"
2+
#include "Euclidean.h"
3+
4+
ChinRemainder::ChinRemainder(int x1, int x2, int m1, int m2) : x1(x1), x2(x2), m1(m1), m2(m2)
5+
{};
6+
7+
bool ChinRemainder::parametersSet()
8+
{
9+
if (x1 == 0 || x2 == 0 || m1 == 0 || m2 == 0)
10+
{
11+
return false;
12+
}
13+
14+
return true;
15+
}
16+
17+
int256_t ChinRemainder::calculate()
18+
{
19+
if (!parametersSet())
20+
{
21+
return 0;
22+
}
23+
24+
int a = x1 * m2;
25+
int b = x2 * m1;
26+
27+
int256_t x, s;
28+
29+
Euclidean::extendedEuclidean(a, m2, &x, &s);
30+
}

ChinRemainder.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//---------------------------------------------------------
2+
// Implementaton of the chinese remainder theorem.
3+
// Includes the basic and extended algorithm of the theorem.
4+
//
5+
// Author: Sebastian Weninger
6+
//
7+
8+
#ifndef CHIN_REMAINDER
9+
#define CHIN_REMAINDER
10+
11+
#include <boost/multiprecision/cpp_int.hpp>
12+
using namespace boost::multiprecision;
13+
14+
class ChinRemainder
15+
{
16+
private:
17+
18+
int x1;
19+
int x2;
20+
21+
int m1;
22+
int m2;
23+
24+
public:
25+
26+
//-------------------------------------------------------
27+
// Constructor
28+
//
29+
ChinRemainder(int x1, int x2, int m1, int m2);
30+
31+
//-------------------------------------------------------
32+
// Query if all parameters are set.
33+
//
34+
// return: bool
35+
//
36+
bool parametersSet();
37+
38+
//-------------------------------------------------------
39+
// Calculate result with initialized modules
40+
//
41+
// return: int265_t result
42+
//
43+
int256_t calculate();
44+
};
45+
46+
#endif

Decryptor.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <string>
2+
#include "Decryptor.h"
3+
#include "RSA.h"
4+
#include <boost/multiprecision/cpp_int.hpp>
5+
6+
using namespace boost::multiprecision;
7+
using namespace Crypto;
8+
using std::string;
9+
10+
Decryptor::Decryptor(const PrivateKey *key) : text(""), private_key(key)
11+
{
12+
};
13+
14+
15+
string Decryptor::decryptString(CryptoString input)
16+
{
17+
string cypher;
18+
19+
int256_t tmp;
20+
int it;
21+
for (it = 0; it < input.size(); it++)
22+
{
23+
tmp = powm(input[it], private_key->s, private_key->q * private_key->p);
24+
cypher.push_back(tmp.convert_to<char>());
25+
}
26+
27+
return cypher;
28+
}
29+
30+
int Decryptor::decryptChar(int c) const
31+
{
32+
int256_t res = powm((int256_t)c, private_key->s, private_key->q * private_key->p);
33+
return res.convert_to<int>();
34+
}

Decryptor.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//---------------------------------------------------------
2+
// Class to decrypt data.
3+
//
4+
// Author: Sebastian Weninger
5+
//
6+
7+
#ifndef DECRYPTOR_
8+
#define DECRYPTOR_
9+
10+
#include "RSA.h"
11+
#include <string>
12+
13+
using std::string;
14+
15+
namespace Crypto
16+
{
17+
class Decryptor
18+
{
19+
private:
20+
const PrivateKey* private_key;
21+
22+
string text;
23+
24+
public:
25+
// ------------------------------------------------------
26+
// Constructor
27+
Decryptor(const PrivateKey* key);
28+
29+
// ------------------------------------------------------
30+
// Decrypts a whole string.
31+
//
32+
// input: input string
33+
// return: cypher string
34+
string decryptString(CryptoString input);
35+
36+
//-------------------------------------------------------
37+
// Decrypts a single char.
38+
//
39+
// c: input char
40+
// return: decrypted char, represented as integer
41+
int decryptChar(int c) const;
42+
};
43+
}
44+
#endif

Encryptor.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string>
2+
#include "Encryptor.h"
3+
#include "RSA.h"
4+
#include <boost/multiprecision/cpp_int.hpp>
5+
6+
using namespace boost::multiprecision;
7+
using namespace Crypto;
8+
using std::string;
9+
10+
Encryptor::Encryptor(const PublicKey* key) : text(""), public_key(key)
11+
{};
12+
13+
CryptoString Encryptor::encryptString(string &input)
14+
{
15+
CryptoString str;
16+
17+
int256_t tmp;
18+
int it;
19+
for (it = 0; it < input.length(); it++)
20+
{
21+
tmp = powm((int256_t)input.at(it), public_key->r, public_key->m);
22+
str.push_back(tmp);
23+
}
24+
25+
return str;
26+
}
27+
28+
int256_t Encryptor::encryptChar(int c) const
29+
{
30+
int256_t res = powm((int256_t)c, public_key->r, public_key->m);
31+
return res.convert_to<int>();
32+
}

Encryptor.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//---------------------------------------------------------
2+
// Class to encrypt data.
3+
//
4+
// Author: Sebastian Weninger
5+
//
6+
7+
#ifndef ENCRYPTOR_
8+
#define ENCRYPTOR_
9+
10+
#include "RSA.h"
11+
#include <string>
12+
13+
using std::string;
14+
15+
namespace Crypto
16+
{
17+
class Encryptor
18+
{
19+
private:
20+
const PublicKey* public_key;
21+
22+
string text;
23+
24+
public:
25+
// ---------------------
26+
// Constructor
27+
//
28+
Encryptor(const PublicKey* key);
29+
30+
// ---------------------
31+
// input: input string
32+
// return: Crypto string
33+
CryptoString encryptString(string& input);
34+
int256_t encryptChar(int c) const;
35+
};
36+
}
37+
#endif

Euclidean.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "Euclidean.h"
2+
3+
#include <boost/multiprecision/cpp_int.hpp>
4+
using namespace boost::multiprecision;
5+
6+
Euclidean::Euclidean()
7+
{};
8+
9+
Euclidean::~Euclidean()
10+
{};
11+
12+
int256_t Euclidean::extendedEuclidean(int256_t a, int256_t b, int256_t* x, int256_t* s)
13+
{
14+
if ((a == 0) || (b == 0))
15+
{
16+
return 0;
17+
}
18+
19+
int256_t q = 0;
20+
int256_t r = 0;
21+
22+
q = a / b;
23+
24+
//remainder
25+
r = a % b;
26+
27+
int256_t x_tmp = *x;
28+
int256_t s_tmp = *s;
29+
if (r != 0)
30+
{
31+
extendedEuclidean(b, r, &x_tmp, &s_tmp);
32+
}
33+
34+
if (r == 0)
35+
{
36+
*x = 0;
37+
*s = 1;
38+
39+
return b;
40+
}
41+
42+
*x = s_tmp;
43+
*s = x_tmp - (q * s_tmp);
44+
45+
return b;
46+
}
47+
48+
int256_t Euclidean::euclidean(int256_t a, int256_t b)
49+
{
50+
51+
if ((a == 0) || (b == 0))
52+
{
53+
return 0;
54+
}
55+
56+
int256_t q;
57+
int256_t r;
58+
59+
do
60+
{
61+
q = a / b;
62+
r = a % b;
63+
64+
a = b;
65+
b = r;
66+
} while (r != 0);
67+
68+
return a;
69+
}

Euclidean.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//---------------------------------------------------------
2+
// Implementation of the basic and extended euclidean algorithm.
3+
// Mainly used for secret key calculation.
4+
//
5+
// Input and output of the algorithms are 256 bit integers from the
6+
// boost library.
7+
//
8+
// Author: Sebastian Weninger
9+
//
10+
11+
#include <boost/multiprecision/cpp_int.hpp>
12+
using namespace boost::multiprecision;
13+
14+
#ifndef EUCLIDEAN_
15+
#define EUCLIDEAN_
16+
17+
class Euclidean
18+
{
19+
private:
20+
// Constructor
21+
Euclidean();
22+
// Desctructor
23+
~Euclidean();
24+
public:
25+
26+
///----------------------------------------------------
27+
// Extended euclidean algorithm to calculate gcd of
28+
// two integers and it's inverse representatives.
29+
//
30+
//
31+
// *x: pointer to inverse of a
32+
// *s: pointer to inverse of b (secret key in cryptography)
33+
//
34+
// return: gcd of a and b
35+
//
36+
static int256_t extendedEuclidean(int256_t a, int256_t b, int256_t* x, int256_t* s);
37+
38+
//-----------------------------------------------------
39+
// Basic euclidean algorithm to calculate gcd of
40+
// two integers.
41+
//
42+
// return: gcd of a and b
43+
//
44+
static int256_t euclidean(int256_t a, int256_t b);
45+
};
46+
47+
#endif
48+

0 commit comments

Comments
 (0)