0% found this document useful (0 votes)
109 views5 pages

Code:: Cryptography Fundamentals Lab Assignment - 4 Rsa Implementation

The document discusses an implementation of RSA cryptography through code that generates encryption and decryption keys, encrypts and decrypts a message string, and includes functions for checking prime numbers, calculating the encryption key, and encrypting/decrypting the message. It also includes code for implementing Diffie-Hellman key exchange to securely generate a shared secret key between two parties.

Uploaded by

Surya Vjkumar
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)
109 views5 pages

Code:: Cryptography Fundamentals Lab Assignment - 4 Rsa Implementation

The document discusses an implementation of RSA cryptography through code that generates encryption and decryption keys, encrypts and decrypts a message string, and includes functions for checking prime numbers, calculating the encryption key, and encrypting/decrypting the message. It also includes code for implementing Diffie-Hellman key exchange to securely generate a shared secret key between two parties.

Uploaded by

Surya Vjkumar
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/ 5

17BCE0147 VIJAY SURYA

CRYPTOGRAPHY FUNDAMENTALS

LAB ASSIGNMENT - 4

RSA IMPLEMENTATION :

code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();

int main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &x);
flag = prime(x);
if(flag == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER SECOND PRIME NUMBER\n");
scanf("%d", &y);
flag = prime(y);
if(flag == 0 || x == y)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");

scanf("%s",msg);
for(i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = x * y;
t = (x-1) * (y-1);
encryption_key();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i = 0; i < j-1; i++)
17BCE0147 VIJAY SURYA
printf("\n%ld\t%ld", e[i], d[i]);
encrypt(msg,e);
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for(i = 2; i <= j; i++)
{
if(pr % i == 0)
return 0;
}
return 1;
}

void encryption_key()
{
int k;
k = 0;
for(i = 2; i < t; i++)
{
if(t % i == 0)
continue;
flag = prime(i);
if(flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if(flag > 0)
{
d[k] = flag;
k++;
}
if(k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while(1)
{
k = k + t;
if(k % a == 0)
return(k / a);
}
}
17BCE0147 VIJAY SURYA
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while(i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\n\nTHE ENCRYPTED MESSAGE IS\n");
for(i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}

void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1)
{
ct = temp[i];
k = 1;
for(j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for(i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
17BCE0147 VIJAY SURYA

output:
17BCE0147 VIJAY SURYA

Diffie- Hellman
code:

#include<stdio.h>
long long int power(int a, int b, int mod) {
int t;
if(b==1)
return a;
t=power(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return(((t*t)%mod)*a)%mod;
}
long long int key(int a, int x, int n) {
return power(a,x,n); }
int main()
{
int n,g,x,a,y,b;
printf("Enter value of base(n) and modulus(g) ");
scanf("%d%d" , &n,&g);
printf("Enter x(secret integer - person 1) for p1 ");
scanf("%d" ,&x);
a=power(g,x,n);
printf("Enter the val of y(secret integer) for 2nd person ");
scanf("%d",&y);
b=power(g,y,n);
printf("first person key %lld\n", power(b,x,n));
printf("second person key %lld\n", power(a,y,n));
}

output:

You might also like