0% found this document useful (0 votes)
40 views

Programming Assingment

The document contains C++ code implementing RSA, Extended Euclidean algorithm, and Elgamal encryption algorithms. It defines functions for key generation, encryption, decryption and includes code to encrypt/decrypt messages by mapping letters to numbers.

Uploaded by

osama ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Programming Assingment

The document contains C++ code implementing RSA, Extended Euclidean algorithm, and Elgamal encryption algorithms. It defines functions for key generation, encryption, decryption and includes code to encrypt/decrypt messages by mapping letters to numbers.

Uploaded by

osama ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Abdalla Adel Abdalla Hadabi

Student ID:201914090115

//this code is to implement RSA code by: Abdalla Adel Abdalla

#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <math.h>
using namespace std;
int p, q, n, e, d, z, m[10] = { 0 }, c[10] = { 0 }, unc[10], m_count;
char ch[] =
{'*','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
'v','w','x','y','z'}, m_ch[10],m_unc[10];
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);

}
void gen_e()
{

for (int i = 2; i < n; i++)


{
if (gcd(i, z) == 1)
{
e = i;
break;
}

void gen_d()
{
int i = 1;
while ((e*i) % z != 1)
{
i++;
}
d = i;
}
int pw(int b, int p)
{
int mlti = 1;
for (int po = 0; po < p; po++)
mlti = mlti*b;
return mlti;

}
void Enc()
{
for (int x = 0; x < m_count; x++)
c[x] = (pw(m[x], e)) % n;

}
void Dec()
{
for (int x = 0; x < m_count; x++)
unc[x] = (pw(c[x], d)) % n;
}
int _tmain(int argc, _TCHAR* argv[])
{

//cout << gcd(8, 24);


p = 5;
q = 7;
n = p*q;
z = (p - 1)*(q - 1);
gen_e();
gen_d();
//cout << d;
cout << "enter your message(no space and not more than 10 letter ) :";

cin >> m_ch;


//find the length of letter
int count = 0;
for (int c = 0; c < 10;c++)
if (m_ch[c] != 0)
{
count++;

m_count = count;
//End finding the length
//substitute letters with numbers
for (int t = 0; t < m_count;t++)
for (int r = 1; r < 27;r++)
if (m_ch[t] == ch[r])
{
m[t] = r;
break;
}
//end of substitution
Enc();
cout << "the encryption is:";
for (int p = 0; p < m_count; p++)
cout << c[p];
cout << "\nthe decryption is:";
Dec();
for (int p = 0; p < m_count; p++)
cout << unc[p];
cout << "\n the message after decoding:";
//substitute letters with numbers
for (int t = 0; t < m_count; t++)
for (int r = 1; r < 27; r++)
if (unc[t] ==r)
{
m_unc[t] = ch[r];
break;
}
cout << m_unc;

Sleep(10000);
return 0;
}
//this code is to implement Extended Euclidean algorithm code by: Abdalla Adel Abdalla
//

#include "stdafx.h"
#include <iostream>
#include <tuple> // std::tuple, std::make_tuple, std::tie
using namespace std;

tuple<int, int, int> extended_gcd(int a, int b)


{
if (a == 0)
return make_tuple(b, 0, 1);

int gcd, x, y;

// unpack tuple returned by function into variables


tie(gcd, x, y) = extended_gcd(b % a, a);

return make_tuple(gcd, (y - (b / a) * x), x);


}

int _tmain(int argc, _TCHAR* argv[])


{
int a = 888;
int b = 312;

tuple<int, int, int> gc = extended_gcd(a, b);

int gcd = get<0>(gc);


int x = get<1>(gc);
int y = get<2>(gc);

cout << "the value of a= " << a<<endl << "the vaule of b="<<b << endl;
cout << "GCD is " << gcd << endl;
cout << "x = " << x << " y = " << y << endl;

cout << a << "*" << x << " + " << b << "*" << y << " = " << gcd;
int q;
cin >> q;
return 0;

}
Elgamal code:

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


//

#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <math.h>
#include<stdlib.h>
using namespace std;
int p, Ephi, g, a, b, k, m[10] = { 0 }, c[10] = { 0 }, s[10] = { 0 },r, unc[10], m_count;
char m_ch[10], m_unc[10] ,ch[] = { '*', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);

}
int pw(int b, int p)
{
int mlti = 1;
for (int po = 0; po < p; po++)
mlti = mlti*b;
return mlti;
}
int modinv(int a, int m)
{
a = a%m;
for (int x = 1; x<m; x++)
if ((a*x) % m == 1)
return x;
}
//**************************************
void Enc()
{

r = (pw(g, k)) % p;
for (int x = 0; x < m_count; x++)
s[x] = (m[x]*modinv(pw(b, k),p) )% p;
}
void Dec()
{
for (int x = 0; x < m_count; x++)
unc[x] = (s[x]*pw(r, a)) % p;
}
//***********************************
int _tmain(int argc, _TCHAR* argv[])
{
p = 11;
Ephi = p - 1;//by assuming p as prime number

//find the group generator


for (int i = 1; i < Ephi;i++)
if (gcd( Ephi,i) == 1)
g = i;

cout << "g="<<g<<endl;


//end
a = rand() %(Ephi-1) + 1;
int pwi= pw(g, a);
b = pwi % p;
cout << "a=" << a <<endl<<"b="<< b<<endl;
k = rand() % (Ephi - 1) + 1;
cout << "k=" << k<< endl;
cout<<"enter your message(no space and not more than 10 letter ) :";
cin >> m_ch;
//find the length of letter
int count = 0;
for (int c = 0; c < 10; c++)
if (m_ch[c] != 0)
{
count++;

}
m_count = count;
//supstitute letters with numbers
for (int t = 0; t < m_count; t++)
for (int w = 1; w < 27; w++)
if (m_ch[t] == ch[w])
{
m[t] = w;
break;
}
//end of supstituation

Enc();
cout << "r=" << r<< endl;
cout << "the encryption is:";
for (int p = 0; p < m_count; p++)
cout << s[p];

cout << "\nthe decryption is:";


Dec();
for (int u = 0; u < m_count; u++)
cout << unc[u];
cout << "\nthe message after decoding:";
//supstitute letters with numbers
for (int t = 0; t < m_count; t++)
for (int v = 1; v < 27; v++)
if (unc[t] == v)
{
m_unc[t] = ch[v];
break;
}
cout << m_unc;
//end of supstituation
Sleep(10000);
return 0;

You might also like