-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.h
83 lines (80 loc) · 3.12 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
#ifndef RSA_H_
#define RSA_H_
#include <string>
#include <fstream>
#include "KeyPair.h"
#include "Key.h"
#include "BigInt.h"
class RSA
{
private:
/* Instantiation of objects of type RSA is forbidden. */
RSA()
{}
/* Copying of objects of type RSA is forbidden. */
RSA(const RSA &rsa);
/* Assignment of objects of type RSA is forbidden. */
RSA &operator=(const RSA &rsa);
/* Returns the greatest common divisor of the two arguments
* "a" and "b", using the Euclidean algorithm. */
static BigInt GCD(const BigInt &a, const BigInt &b);
/* Solves the equation
* d = ax + by
* given a and b, and returns d, x and y by reference.
* It uses the Extended Euclidean Algorithm */
static void extendedEuclideanAlgorithm( const BigInt &a,
const BigInt &b,
BigInt &d,
BigInt &x,
BigInt &y);
/* Solves the equation
* ax is congruent to b (mod n),
* given a, b and n finds x. */
static BigInt solveModularLinearEquation( const BigInt &a,
const BigInt &b,
const BigInt &n);
/* Throws an exception if "key" is too short to be used. */
static void checkKeyLength(const Key &key);
/* Transforms a std::string message into a BigInt message. */
static BigInt encode(const std::string &message);
/* Transforms a BigInt cyphertext into a std::string cyphertext. */
static std::string decode(const BigInt &message);
/* Encrypts a "chunk" (a small part of a message) using "key" */
static std::string encryptChunk(const std::string &chunk,
const Key &key);
/* Decrypts a "chunk" (a small part of a message) using "key" */
static std::string decryptChunk(const BigInt &chunk,
const Key &key);
/* Encrypts a string "message" using "key". */
static std::string encryptString( const std::string &message,
const Key &key);
/* Decrypts a string "message" using "key". */
static std::string decryptString( const std::string &cypherText,
const Key &key);
/* Tests the file for 'eof', 'bad ' errors and throws an exception. */
static void fileError(bool eof, bool bad);
public:
/* Returns the string "message" RSA-encrypted using the key "key". */
static std::string Encrypt( const std::string &message,
const Key &key);
/* Encrypts the file "sourceFile" using the key "key" and saves
* the result into the file "destFile". */
static void Encrypt(const char *sourceFile,
const char *destFile,
const Key &key);
/* Decrypts the file "sourceFile" using the key "key" and saves
* the result into the file "destFile". */
static void Decrypt(const char *sourceFile,
const char *destFile,
const Key &key);
/* Returns the string "cypherText" RSA-decrypted
* using the key "key". */
static std::string Decrypt( const std::string &cypherText,
const Key &key);
/* Generates a public/private keypair. The keys are retured in a
* KeyPair. The generated keys are 'digitCount' or
* 'digitCount' + 1 digits long. */
static KeyPair GenerateKeyPair( unsigned long int digitCount,
unsigned long int k = 3);
};
#endif /*RSA_H_*/