This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RSA.cpp
58 lines (47 loc) · 1.41 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
#ifndef RSA_CPP
#define RSA_CPP
#include "RSA.h"
using namespace std;
void RSA::BigPrime(mpz_class& p, gmp_randclass& rng, unsigned long sz, unsigned long c)
{
while(true)
{
p = rng.get_z_bits(sz);
if(mpz_probab_prime_p(p.get_mpz_t(), c))
break;
}
return;
}
mpz_class RSA::BigEncrypt(mpz_class& Modulus, mpz_class& Key, mpz_class& Msg)
{
mpz_class Cipher;
mpz_powm(Cipher.get_mpz_t(), Msg.get_mpz_t(), Key.get_mpz_t(), Modulus.get_mpz_t());
return Cipher;
}
mpz_class RSA::BigDecrypt(mpz_class& Modulus, mpz_class& Key, mpz_class& Cipher)
{
mpz_class BigMsg;
mpz_powm(BigMsg.get_mpz_t(), Cipher.get_mpz_t(), Key.get_mpz_t(), Modulus.get_mpz_t());
return BigMsg;
}
void RSA::KeyGenerator(mpz_class& Dec, mpz_class& Enc, mpz_class& Mod, gmp_randclass& rng)
{
mpz_class PrimeP = 0;
mpz_class PrimeQ = 0;
mpz_class EulersTot = 0;
Mod = 0;
BigPrime(PrimeP, rng, 2048, 24);
BigPrime(PrimeQ, rng, 2048, 24);
//Set Modulus
Mod = PrimeP * PrimeQ;
EulersTot = (PrimeP - 1) * (PrimeQ - 1);
mpz_xor(PrimeP.get_mpz_t(), PrimeP.get_mpz_t(), PrimeP.get_mpz_t());
mpz_xor(PrimeQ.get_mpz_t(), PrimeQ.get_mpz_t(), PrimeQ.get_mpz_t());
Enc = mpz_class(65537);
Dec = 0;
//Set Decryption Key (Private)
mpz_invert(Dec.get_mpz_t(), Enc.get_mpz_t(), EulersTot.get_mpz_t());
mpz_xor(EulersTot.get_mpz_t(), EulersTot.get_mpz_t(), EulersTot.get_mpz_t());
return;
}
#endif