Programming Assingment
Programming Assingment
Student ID:201914090115
#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()
{
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[])
{
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;
int gcd, x, y;
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:
#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
}
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];