You are on page 1of 8

// RSA.cpp : Defines the entry point for the console application.

//
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<NTL/ZZ.h>
#include<ctype.h>
using namespace std;
using namespace NTL;
int cmmdc(int a, int b)
{
if (b == 0)
return a;
else return cmmdc(b, a % b);
}
int Euclid_extins(int a, int b, int &lastx, int &lasty)
{
int x = 0, y = 1, q, aux, tx, ty;
lastx = 1;
//lasty = 0;
while (b != 0)
{
q = a / b;
aux = b; b = a%b; a = aux;
tx = x; x = lastx - q*x; lastx = tx;
ty = y; y = lasty - q*y; lasty = ty;
}
return a;
}

int mul_inv(int a, int b)


{
int b0 = b, t, q;
int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
int scadere(int a, int b)
{
while (a>b)
a = a - b;
return a;
}
int prim(int a)
{
int i, contor = 0;
for (i = 2; i < a / 2; i++)
if (a%i == 0) contor++;

if (contor == 0)
return 1;
else
return 0;
}
// ===========================================================
long witness(const ZZ& n, const ZZ& x)
{
ZZ m, y, z;
long j, k;
if (x == 0) return 0;
// compute m, k such that n-1 = 2^k * m, m odd:
k = 1;
m = n / 2;
while (m % 2 == 0) {
k++;
m /= 2;
}
z = PowerMod(x, m, n); // z = x^m % n
if (z == 1) return 0;
j = 0;
do {
y = z;
z = (y*y) % n;
j++;
} while (j < k && z != 1);
return z != 1 || y != n - 1;
}
long PrimeTest(const ZZ& n, long t)
{
if (n <= 1) return 0;
// first, perform trial division by primes up to 2000
PrimeSeq s; // a class for quickly generating primes in sequence
long p;
p = s.next(); // first prime is always 2
while (p && p < 2000) {
if ((n % p) == 0) return (n == p);
p = s.next();
}
// second, perform t Miller-Rabin tests
ZZ x;
long i;
for (i = 0; i < t; i++) {
x = RandomBnd(n); // random number between 0 and n-1

if (witness(n, x))
return 0;
}
return 1;
}

//==========================================================
int v[100], n, p, fi, x, a, c, d, e, f, test, g = 0, xx, yy,gg, ggg;
long lungime = 10;
int main()
{
time_t t;
time(&t);
NTL::SetSeed(NTL::to_ZZ((unsigned long)(t + 15)));
int test = 2;
ZZ auxp,auxq,p, q, fi,x , aux, e, d, c, s,n,b,y,mesaj,cript,qq,dp,dq,cp,
cq,mp,mq;
const ZZ gggg,qqq;
//GenPrime(p, 15);
//GenPrime(q, 15);
p = 23;
q = 19;
auxp = p;
auxq = q;
cout << p << "<--p si q-->" << q<<endl;
n = p*q;
cout <<n<< " <--n" << endl;
fi = (p - 1)*(q - 1);
cout <<fi<< " <--fi" << endl;
test = 2;
a = conv<int>( fi);
cout << a<<endl;
cout << "ACILISEA INTRU IN UAILE" << endl;
while (test)
{
srand(conv<int>(y)+135224);
y = RandomBnd(to_ZZ(a));
cout << y << " <--y" << endl;
if (GCD(y, fi) == 1)
test = 0;
}
gg = conv<int>(y);
ggg = conv<int>(fi);
d = InvMod(gg, ggg);
cout << InvMod(gg,ggg);
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<

"\n\n\n Cheie publica :" << n << "," << y;


"\n Cheie privata :" << d ;
"\n\n\n";
"dati mesajul intre 0 si "<<n-1;
"\n";

cin >> mesaj;


ggg = 1;
cript = PowerMod(to_ZZ(mesaj%n), conv<long>(y),to_ZZ(n));
cout << cript<<" <--cript"<<endl;
cout << PowerMod(to_ZZ(cript%n), conv<long>(d), to_ZZ(n)) << endl;
cout << "q si p " << q << " " << p << endl;
dp
dq
cp
cq
mp
mq
ZZ
if
{

= d % (p - 1);
= d % (q - 1);
= cript % p;
= cript % q;
= PowerMod(to_ZZ(cp), conv<long>(dp), to_ZZ(p));
= PowerMod(to_ZZ(cq), conv<long>(dq), to_ZZ(q));
A,max,min;
(p > q)
min = q;
max = p;

}
else
{
max = q;
min = p;
}
A = InvMod(p%q, q);
cout << (((mq + q - mp)*A) % q)*p + mp;
//----------------------------------------------------------------time_t tt;
ZZ ee, dd, ll;
cout << "\nacilisea e codu lu ariton" << endl;
test = 0;
time(&tt);
NTL::SetSeed(NTL::to_ZZ((unsigned long)(tt + 15)));
while (test == 0)
{
int test2 = 0;
ee = RandomBnd(n);
time(&tt);
NTL::SetSeed(NTL::to_ZZ((unsigned long)(tt + 15)));
while (test2 == 0)
{
dd = RandomBnd(n);
if (dd < int((sqrt(sqrt(conv<long>(n)))) / 3))
{
test2 = 1;
cout << int((sqrt(sqrt(conv<long>(n)))) / 3);
}
}
if ((ee*dd) % n == 1)
{
test = 1;
cout << ee*dd << "\n";
}
cout << ee << "---><---" << dd << "\n";

}
cout << ee << " ------------" << dd;
//----------------------------------------------------------------/*
cout << n << endl;
test = 0;
time_t tt;
time(&tt);
NTL::SetSeed(NTL::to_ZZ((unsigned long)(tt + 15)));
while (test == 0)
{
srand(conv<int>(qq)+1);
qq = to_ZZ(RandomBnd(n) % (n / 2));
cout << "cheia random -->" << qq << endl;
if (qq != 0 && PrimeTest(qq,1))
{
if ((n / qq > qq) && (n / qq < 2 * qq) && (n / qq *qq ==
n) && (PrimeTest(n / qq,1)))
test = 1;
}
if (test == 1)
cout << qq << "*" << n / qq << endl;
}
*/
//----------------------------------------------/*
while (test)
{
void RandomBnd(ZZ& x, const ZZ& a);
cout << x << "<--" << endl;
if (GCD(e, fi) == 1)
test = 0;
}
cout << x << " <--x" << endl;
*/
/*
srand(time(NULL));
cout << "p ";
cin >> p;
cout << "q ";
cin >> q;
n = p*q;
fi = (p - 1)*(q - 1);
test = 2;
while (test != 1)
{
x = rand() % fi;
cout << fi << "<-->" << x << endl;
test=cmmdc(fi, x);
cout << "cmmdc->" << cmmdc(fi, x) << endl;
}

e = x;
int auxx = x,auxfi=fi;
d = 24;
cout << Euclid_extins(auxx, 1, c, d);
cout << "\n here" << c << "|||" << d << endl;
cout << d << "<-----------d";
int dd = abs(d) ;
cout << "aici e elementul d " << abs(d) << endl;
cout << "asta e fi--> " << fi << endl;
cout << e*dd << endl;;
cout << (e*dd) % fi;
cout << "==========================" << endl;
cout << mul_inv(e, fi) << endl;
cout << "e=" << e << " d=" << mul_inv(e, fi);
d = mul_inv(e, fi);
int mesaj, cript,criptb,cb;
cout << "\n\n\n";
cout << "cheie publica " <<n<<" , "<<e<< endl;
cout << "cheie privata " << d;
cout << "\n\n\n";
cout << "dati un numar intre 0 si " << n;
cout << endl;
cin >> mesaj;
cript = pow(mesaj, e);
cout << cript << "<---- \n";
c = scadere(cript, n);
cout << "codul criptat este " << c << endl;
cout << endl;
cout << "\n\n\n";
int mm;
cb = pow(c, d);
criptb = cb%n;
cout << criptb << "<-- mesajul initial " << endl;
*/
/*
cin >> n;
srand(time(NULL));
q = 1;
test = 0;
while (test==0)
{
q = rand() % (n / 2);
cout << "cheia random -->" << q << endl;
if (q!=0 &&prim(q))
{
if ((n / q > q) && (n / q < 2 * q) && (n / q *q == n) &&(prim(n/q)))
test = 1;
}
if (test==1)
cout << q << "*" << n / q << endl;
}
p = n / q;
fi = (p - 1)*(q - 1);

*/
/*
cin >> n;
fi = 2000;
int crevete;
crevete = sqrt(sqrt(n))/3;
cout << "\n" << crevete<<endl;
test = 1;
srand(time(NULL));
while (test)
{
//
srand(time(NULL));
d = rand() % crevete;
srand(time(NULL));
cout << d << endl;
if (d > 3 && prim(d))
{
e = mul_inv(d, fi);
test = 0;
}
}
cout << "e-ul cautat este " << e << endl;
int l = (e*d - 1)/fi;
cout << l <<"<--- l "<< endl;
cout << "d= " << d;
*/
/*
e = 4;
d = 11;
int i, auxe, auxd, contor, r;
float s = 0, initial;
auxe = e;
auxd = d;
i = 0;
v[i] = auxe / auxd;
r = auxe - v[i] * auxd;
auxe = auxd;
auxd = r;
while (r)
{
i++;
v[i] = auxe / auxd;
r = auxe - v[i] * auxd;
auxe = auxd;
auxd = r;
}
cout << endl;
for (int j = 0; j <= i; j++)
cout << v[j] << " ";
cout << endl;
cout << "\n\n\n";
//cout << l << "--" << d;
cout << "\n\n\n";
x = i - 1;
initial = (float)1 / v[x + 1];

while (x>0)
{
s = (float)1 / (v[x] + initial);
cout << "\n" << s << "<--suma dupa fiecare pas " << x + 1 << " c
u initial " << initial << " si floatul " << v[x] << "+ " << initial;
initial = (float)1 / (v[x] + initial);
x--;
}
cout << "\n" << s + v[0] << " <---suma " << endl;
ZZ a,b,y;
a = 16;
b = 20;
ZZ c = to_ZZ(37);
cout << SqrRoot(a);
cout << GCD(16, 20);
cout << "\n\n";
ZZ t,x,prime;
srand(time(NULL));
prime = 2;
//while (prime < 50000) {
//
cout << prime << " ";
//
prime = NextPrime(prime + 1);
//}
GenPrime(x, 17);
cout << endl;
GenPrime(y, 18);
cout << x<<"---"<<y<<"*= "<<x*y;
cout << endl;
*/
return 0;
}

//to compute a range

You might also like