You are on page 1of 9

1.

Implement Fast Exponent Method for the below given values


a. 342 raise to 1231 with modulus of 7789 (342 pow 1231 Mod 7789)

CODE:
#include <stdio.h>

int power(int a, unsigned int b, int n)


{
int res = 1;

a = a % n;
while (b > 0)
{

if (b & 1)
res = (res*a) % n;

b= b>>1; // b = b/2
a = (a*a) % n;
}
return res;
}

int main()
{
int a,b,n;
printf("Enter the base, power and modulus:");
scanf("%d%d%d",&a,&b,&n);
printf("%d^%dmod%d is %u\n",a,b,n, power(a, b, n));
return 0;
}
OUTPUT:
2. Implement Euler totient/phi Function for the below value
a.1228
CODE:
#include <stdio.h>

int gcd(int a, int b)


{
if (a == 0)
return b;
return gcd(b % a, a);
}
int phi(unsigned int n)
{
unsigned int result = 1;
for (int i = 2; i < n; i++)
if (gcd(i, n) == 1)
result++;
return result;
}
int main()
{
int n;
printf("Enter the number:");
scanf("%d",&n);
printf("phi(%d) = %d\n", n, phi(n));
return 0;
}
OUTPUT:
3. Find the Modular Multiplicative inverse of 325 with modulus value 7789 by using Extended
Euclidean Method.

CODE:
#include<iostream>
using namespace std;

int gcdExtended(int a, int b, int *x, int *y);

void modInverse(int a, int m)


{
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else
{
int res = (x%m + m) % m;
cout << "Modular multiplicative inverse is " << res;
}
}

int gcdExtended(int a, int b, int *x, int *y)


{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}

int x1, y1;


int gcd = gcdExtended(b%a, a, &x1, &y1);

*x = y1 - (b/a) * x1;
*y = x1;

return gcd;
}

int main()
{
int a,m;
printf("\n Enter a and m:");
scanf("%d%d",&a,&m);
modInverse(a, m);
return 0;
}

OUTPUT:
4. RSA
a. Find the private key with below give values
i. Prime1 p=73
ii. Prime2 q=227
iii. Public key e=29
iv. Find Private =?
b. Encrypt the Plain text IIT (ASCII Values) and Decrypt the generated cipher with above
Public and private keys respectively.

CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int encrypt(int);
int decrypt(int);
int prime(int);
int expo(int,int,int);
int n,phi,p,q,e,d;
int main()
{
int i,s,m,c;
printf("***RSA IMPLEMENTATION***");
printf("\n Enter two prime numbers:");
scanf("%d%d",&p,&q);
n=p*q;
phi=(p-1)*(q-1);
printf("\n phi value is:%d",phi);
printf("\n Enter the value of e which is less than phi:");
scanf("%d",&e);
if(prime(p)&&prime(q)&&prime(e))
{
d=1;
do
{
s=(d*e)%phi;
d++;
}
while(s!=1);
d=d-1;
printf("\n Public Key:(%d,%d)",e,n);
printf("\n Private Key:(%d)",d);
printf("\n **ENCRYPTION**");
printf("\n Plain Text:");
scanf("%d",&m);
encrypt(m);
printf("\n **DECRYPTION**");
printf("\n Cipher text:");
scanf("%d",&c);
decrypt(c);
printf("\n");
}
else
return 0;
}

int encrypt(int m)
{
int c;
c=expo(m,e,n);
printf("\n Encrypted Text:%d",c);
}

int decrypt(int c)
{
int m;
m=expo(c,d,n);
printf("\n Decrypted Text:%d",m);
}

int expo(int a,int b,int n)


{
int d=1,i=0,q=b;
int bits[100];
while(q!=0)
{
bits[i]=q%2;
i++;
q=q/2;
}
while(i>=0)
{
d=(d*d)%n;
if(bits[i]==1)
{
d=(a*d)%n;
}
i--;
}
return d;
}

int prime(int pr)


{
int i,j;
j=pr/2;
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;
}

OUTPUT:

You might also like