Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

COLLEGE OF APPLIED BUSINESS

Chabahil, Gangahity, Kathmandu

Lab Assignments for partial fulfillment of


CSC: 316 Cryptography
PRACTICAL EXAM-2078

Submitted by

Dipak Giri (20525)


College of Applied Business
B.Sc. CSIT 5th Semester

Submitted to

Mr. Santosh Sharma


Department of Computer Science & Information Technology
College of Applied Business

April 5, 2022
COLLEGE OF APPLIED BUSINESS
Chabahil, Gangahity, Kathmandu

CERTIFICATE OF AUTHENTICATION

This is to verify that Mr. /Miss.……………… student of B.Sc.-CSIT 5th


semester, has submitted assignment report for his practical fulfillment of subject
CSC: 316 Cryptography practical exam-2078. His Roll number
is…………………………

……………….. ……………………

Subject Teacher External Examiner


Santosh Sharma ..................................
LAB-8 IMPLEMENTATION OF RSA ALGORITHM
AIM:
To implement the RSA algorithm using asymmetric cryptographic algorithm.

DESCRIPTION:
RSA (Rivest-sharmir-adleman) is an algorithm used by modern computers to encrypto and
decrypt messages. it is an asymmetric cryptographic algorithm, asymmetric means that there
are two different keys. that is private key and public key .

ALGORITHM:

RSA algorithm uses the following procedure to generate public and private
keys:

1. Select two large prime numbers, p and q.

2. Multiply these numbers to find n = p x q, where n is called the modulus for encryption
and decryption.
3. Choose a number e less than n, such that n is relatively prime to (p - 1) x (q -1). It means
that e and (p - 1) x (q - 1) have no common factor except 1. Choose "e" such that1<e<φ
4. If n = p x q, then the public key is <e, n>. A plaintext message m is encrypted using
public key <e, n>. To find cipher text from the plain text following formula is used to
ciphertext
C=me mod n Here, m must be less than n. A larger message (>n) is treated as a
concatenation of messages, each of which is encrypted separately.
5. To determine the private key, we use the following formula to calculate the d such that:
De mod{(p-1)x(q-1)}=1
Or
De mod φ (n) = 1
6. The private key is <d, n>. A ciphertext message c is decrypted using private key <d, n>.
To calculate plain text m from the ciphertext c following formula is used to get plain text
m.
m = cd mod n
PROGRAM: (RSA ALGORIHM)
#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++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
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); }}
void encrypt() {
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");
}

OUTPUT:
LAB-9 IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE
ALGORITHM
AIM:
Implementing Diffie Hellman Key Exchange Algorithm Whitefield Diffie and Martin
Hellman devised an solution to the problem of key agreement or key exchange in 1976. This
solution is called as Diffie-Hellman key exchange / Agreement Algorithm.

DESCRIPTION:
Diffie hellman algorithm is a public key algorithm used to established a shared secret that can
be used for secret communication while exchanging data over a public network.
Aside from using the algorithm for generating public keys, there are some other
places where DH Algorithm can be used: Encryption: The Diffie Hellman key exchange
algorithm can be used to encrypt; one of the first schemes to do is ElGamal encryption.

ALGORITHM:
1. key =(YA)XBmod q -> this is the same as calculated by B

2. Global Public Elements


q: q is a prime number
a: a < q and α is the primitive root of q

3. Key generation for user A


Select a Private key XA
Here, XA <q
Now, Calculation of Public key YA YA = aXA mod q

4. Key generation for user B


Select a Private key XB
Here, XB <q
Now, Calculation of Public key YB
YB = aXb mod q

5. Calculation of Secret Key by A


key =(YB)XA mod q

6. Calculation of Secret Key by B


key =(YA)XB mod q
PROGRAM: (Diffie hellman algorithm implementation)
#include<stdio.h>
#include<conio.h>
long long int power(int a, int b, int mod) {
long long 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 int calculateKey(int a, int x, int n) {
return power(a, x, n);}
void main() {
int n, g, x, a, y, b;
clrscr();
printf("Enter the value of n and g : ");
scanf("%d%d", & n, & g);
printf("Enter the value of x for the first person : ");
scanf("%d", & x);
a = power(g, x, n);
printf("Enter the value of y for the second person : ");
scanf("%d", & y);
b = power(g, y, n);
printf("key for the first person is : %lld\ n ",power(b,x,n));
printf("key for the second person is : %lld\ n ",power(a,y,n));
getch();
}

OUTPUT:

You might also like