Professional Documents
Culture Documents
CODE:
#include <stdio.h>
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>
CODE:
#include<iostream>
using namespace std;
*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);
}
OUTPUT: